SWAP


 

The SWAP instruction exchanges the contents of the source operand with the contents of the destination operand. The operands must be compatible for a swap operation. In general, this means that the operands must have the same variable type, physical size, and any required format or state attributes. The instruction uses the following format:

 

 

[label]

SWAP

{source}{sep}{dest}

 

Where:

label

Optional. A Program Execution Label.

source

Required. A previously defined Character String Variable, National Character String Variable, or Numeric Variable. In addition, the source operand can be a RECORD or VARLIST that contains multiple variables to be used for multiple swap operations.

sep

Required. A comma or one of the following prepositions: BY, TO, OF, FROM, USING, WITH, IN, or INTO.

dest

Required. A previously defined Character String Variable, National Character String Variable, or Numeric Variable. In addition, the source operand can be a RECORD or VARLIST that contains multiple variables to be used for multiple swap operations.

Flags Affected: OVER

Note the following:

  1. By default, the OVER flag is cleared. The OVER flag is set TRUE if a GUI object is encountered and skipped in a RECORD to RECORD swap operation. When this occurs, the GUI object member is no swapped, the operation continues with the next variable, and the OVER flag is set.

  2. If the source and destination RECORD/VARLIST operands contain a different number of variables, SWAP processes matching variables in order until the shorter variable list is exhausted or until an error is encountered.

  3. When the SWAP operation detects an error, an F18 error is generated and the subcode identifies the specific type of error encountered as follows:

  4.  

    Sub Code Value

    Source/destination variable mismatch because:

    1

    The types are not the same.

    2

    An operand variable with an unknown type is encountered.

    3

    The variable behavior/state attributes required for the swap are not the same.

    5

    The physical sizes are not the same.

    6

    A DIM NULL variable is not allowed.

    7

    Unable to allocate memory when creating a buffer for a variable larger than 8192 bytes.

    8

    The form formats are not the same.

    9

    The GUI object types are not the same.

     

  5. The following example demonstrates SWAP processing for individual variables, RECORD variables, and VARLIST variables:

 

************************************************************

. Individual DIM SWAP operation

.

A DIM 10

B DIM 10

.

        MOVE "abcdef", A

        MOVE "rst", B

        DISPLAY A, "...", B, " (Before SWAP )"

.

        DISPLAY "SWAP A with B"

        SWAP A with B

.

        DISPLAY A, "...", B, " (After SWAP )",*N

.

************************************************************

. Individual DIM pointer SWAP operation

.

pA DIM ^

pB DIM ^

.

        DMAKE pA, 15

        DMAKE pB, 15

        MOVE "1234", pA

        MOVE "567890", pB

        DISPLAY pA, "...", pB, " (Before SWAP )"

.

        DISPLAY "SWAP pA, pB"

        SWAP pA, pB

.

        DISPLAY pA, "...", pB, " (After SWAP )",*N

.

************************************************************

. Individual FORM SWAP operation

.

F5p4 FORM 5.4

F5p4a FORM 5.4

.

        MOVE "12.34", F5p4

        MOVE "98765.4321", F5p4a

        DISPLAY F5p4, "...", F5p4a, " (Before SWAP )"

.

        DISPLAY "SWAP F5p4, F5p4a"

        SWAP F5p4, F5p4a

.

        DISPLAY F5p4, "...", F5p4a, " (After SWAP )",*N

.

************************************************************

. SWAP operation using RECORDs

.

RecDef RECORD DEFINITION

A DIM 15

B FORM 2.5

C INTEGER 4

        RECORDEND

.

Rec1 RECORD LIKE RecDef

Rec2 RECORD LIKE RecDef

.

Data1 INIT "abcdefg;3.456"

Data2 INIT "ABCDEFGHIJKLMN;12.34567"

.

        EXPLODE Data1, ";", Rec1

        EXPLODE Data2, ";", Rec2

        MOVE 8192, Rec1.C

        MOVE 16384, Rec2.C

.

        DISPLAY "Rec1 '", Rec1, "' (Before SWAP)", *N:

                   "Rec2 '", Rec2, "' (Before SWAP)"

.

        DISPLAY "SWAP Rec1, Rec2"

        SWAP Rec1, Rec2

.

        DISPLAY "Rec1 '", Rec1, "' (After SWAP)", *N:

                   "Rec2 '", Rec2, "' (After SWAP)", *N

.

**********************************************************

. SWAP operation using RECORD and Object

.

RecDefx RECORD DEFINITION

A DIM 15

B FORM 2.5

Combo COMBOBOX

C INTEGER 4

        RECORDEND

.

List3 LIST

Rec3 RECORD LIKE RecDef

        LISTEND

.

List4 LIST

Rec4 RECORD LIKE RecDef

        LISTEND

.

Data3 INIT "Welcome;3.456"

Data4 INIT "To my home!;12.34567"

.

        EXPLODE Data3, ";", Rec3

        EXPLODE Data4, ";", Rec4

        MOVE 4096, Rec3.C

        MOVE 8192, Rec4.C

.

        DISPLAY "Rec3 '", Rec3, "' (Before SWAP)",*N:

                   "Rec4 '", Rec4, "' (Before SWAP)"

.

        DISPLAY "----->SWAP List3, List4"

        SWAP List3, List4

        IF OVER

         DISPLAY "OVER is TRUE --> Object skipped!!"

        ENDIF

.

        DISPLAY "Rec3 '", Rec3, "' (After SWAP)", *N:

                   "Rec4 '", Rec4, "' (After SWAP)", *N

.

************************************************************

.

        KEYIN "Hit enter to exit: ", S$CMDLIN

.

 

 

 

See Also: Character String Instructions

 



PL/B Language Reference