SWAP statement

Purpose:

SWAP Syntax 1: exchanges the values or contents of two variables of the same data type. Syntax 2: exchanges, between two strings, substrings of a specified length located at specified positions.

Syntax 1:

SWAP Variable1, Variable2

Parameters:

  • Data type: Identifier
    Variable1 Variable to swap with Variable2.
  • Data type: Identifier
    Variable2 Variable to swap with Variable1.
  • 👉Variable1 and Variable2 must be variables of the same data type.

Syntax 2:

SWAP (PBYTE) &StrOne[P1] AS STRING, (PBYTE) &StrTwo[P2] AS STRING, Length AS INTEGER)

Parameters:

  • Data type: STRING
    &StrOne[P1] String containing substring to be swapped with substring of StrTwo. [P1] is the beginning position of the StrOne substring.
  • Data type: STRING
    &StrTwo[P2] String containing substring to be swapped with substring of StrOne. [P2] is the beginning position of the StrTwo substring.
  • Data type: INTEGER
    Length The length of the substring to be swapped.

Example:

This example exchanges
4 characters, "1234", beginning at position 0 of a$
with
4 characters, "4321", beginning at position 6 of b$.

DIM a$
DIM aWas$
DIM b$
DIM bWas$
 
a$ = "1234567890"
b$ = "0987654321"
aWas$ = a$
bWas$ = b$

SWAP (PBYTE) &a$[0], (PBYTE) &b$[6], 4

PRINT "a$ before SWAP was ", aWas$
PRINT "a$ after SWAP is   ", a$
PRINT
PRINT "b$ before SWAP was ", bWas$
PRINT "b$ after SWAP is   ", b$

Result:

a$ before SWAP was 1234567890
a$ after SWAP is   4321567890

b$ before SWAP was 0987654321
b$ after SWAP is   0987651234

Remarks:

The following statements are functionally equivalent. The first is verbose, the second is not but it might look more cryptic to some users.

SWAP CAST(PBYTE, ADDRESSOF Mut$[i]), CAST(PBYTE, ADDRESSOF Mut$[CAST(LONG, Range(i+1, L-1))]), 1
               
SWAP (PBYTE) &Mut$[i], (PBYTE)&Mut$[( LONG) Range(i + 1,L - 1)], 1

BCX Console Sample Programs using the SWAP function.