SWAP - mkilgore/QB64pe GitHub Wiki

The SWAP statement is used to exchange two variable or array element values.

Syntax

SWAP variable1, variable2

Description

  • variable1 and variable2 are any type variables whose values will be exchanged.
  • If either variable1 or variable2 is an array, then an element in the array must be designated.
  • SWAP can be used with string or number variable values. Both must be of the same type.
  • SWAP is often used to sort array elements into greater or lesser numerical or cumulative ASCII STRING values.
  • SWAP can be used in page flipping to change between source and destination pages.
Example 1: A simple SWAP of string values.
a$ = "one"
b$ = "two"

SWAP a$, b$

PRINT a$
PRINT b$
two
one

Example 2: Creating Cryptograms by scrambling EVERY capital letter in the alphabet.

COLOR 11: LOCATE 10, 10
FOR...NEXT i = 65 TO 90
  IF...THEN Letter$(i) = CHR$(i) THEN      'find characters the same as the ASCII code index
    DO...LOOP: j = INT(RND * 26) + 65: LOOP WHILE j = i    'loop until j <> i
    SWAP Letter$(i), Letter$(j)     'swap corresponding letter characters
  END IF
  PRINT CHR$(i); " ";               'print normal alphabetical order
NEXT

COLOR 14: LOCATE 12, 10
FOR...NEXT a = 65 TO 90                    'display new alphabetical order
  PRINT Letter$(a); " ";
NEXT

text$ = "This is how a normal sentence would look before being encrypted."
COLOR 11: LOCATE 20, 5: PRINT text$
L = LEN(text$)
DIM Code(L)                         'place ASCII code solution into an array
COLOR 14: LOCATE 22, 5
FOR...NEXT i = 1 TO L
  Code(i) = ASC(UCASE$(text$), i)   'in QB64, ASC can read by character position
  IF...THEN Code(i) >= 65 AND (boolean) Code(i) <= 90 THEN PRINT Letter$(Code(i)); ELSE PRINT CHR$(Code(i));
NEXT
END '' ''
Code by Ted Weissgerber
Explanation: The Letter$ STRING array is first created with the letters matching the ASCII code index value. Every index is swapped when the letter matches it's index code until every letter is different. The Code array holds the text code solution.
Example 3: A very quick array sorting SUB procedure using recursion sorts 10 thousand numbers in milliseconds.
SUB QuickSort (start AS INTEGER, finish AS INTEGER, array() AS SINGLE)    
DIM Hi AS INTEGER, Lo AS INTEGER, Middle AS SINGLE
Hi = finish: Lo = start
Middle = array((Lo + Hi) / 2) 'find middle of array
DO
  DO WHILE array(Lo) < Middle: Lo = Lo + 1: LOOP
  DO WHILE array(Hi) > Middle: Hi = Hi - 1: LOOP
  IF Lo <= Hi THEN
    SWAP array(Lo), array(Hi)
    swap2 = swap2 + 1                  'count swaps for demo only    
    Lo = Lo + 1: Hi = Hi - 1
  END IF                               'If homework, you will fail
LOOP UNTIL Lo > Hi
IF Hi > start THEN CALL QuickSort(start, Hi, array())
IF Lo < finish THEN CALL QuickSort(Lo, finish, array())
END SUB '' ''
 array(0)= 0.20200    array(5000)= 525.8505   array(10000)= 999.6196
 Elapsed time: 0.023438 seconds with 33,759 swaps
NOTE: The swap2 shared value is used to count the swaps for the demo and can be removed from the SUB procedure for speed.
See also:
Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page
⚠️ **GitHub.com Fallback** ⚠️