|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > Arrays and Data-Structures > ARS. - Array's > !ARS. - Array Sorting > ARS. - Array Operations |
MiniRobotLanguage (MRL)
ARS.Swap
Swaps two elements in an array based on their indices.
Intention
The ARS.Swap command exchanges the positions of two elements within an array ($$ARR) at specified indices ($$INA and $$INB).
The array can be Integer, FP, or String, and the swap is performed in-place, directly modifying $$ARR.
Illustration:
📦 Before Swap:
$$ARR: [1, 2, 3]
🔄 After Swap (indices 1 and 3):
$$ARR: [3, 2, 1]
Syntax
ARS.Swap|$$ARR|$$INA|$$INB
Parameter Explanation
P1 - $$ARR - (Variable)
The handle of the array containing the elements to swap (Integer, FP, or String).
P2 - $$INA - (Variable)
The index of the first element to swap (1-based).
P3 - $$INB - (Variable)
The index of the second element to swap (1-based).
Example
'***********************************
' ARS.Swap - Sample 1
'***********************************
ARS.New|$$ARR|i
ARS.Add|$$ARR|1
ARS.Add|$$ARR|2
ARS.Add|$$ARR|3
VAR.$$INA=1
VAR.$$INB=3
ARS.Swap|$$ARR|$$INA|$$INB
DBP.Swapped $$ARR: [3, 2, 1]
ARS.End|$$ARR
'
'***********************************
' ARS.Swap Testing Block
' Tests swapping elements in arrays of different types
'***********************************
' Test 1: Integer Array Swap
ARS.New|$$ARR|Integer
ARS.Add|$$ARR|10
ARS.Add|$$ARR|20
ARS.Add|$$ARR|30
ARS.Add|$$ARR|40
DBP.Before swap: [10, 20, 30, 40]
VAR.$$INA=1
VAR.$$INB=3
ARS.Swap|$$ARR|$$INA|$$INB
ARS.Get|$$ARR|1|$$VAL
DBP.After swap 1 and 3: $$VAL (Expected: 30)
ARS.Get|$$ARR|3|$$VAL
DBP. : $$VAL (Expected: 10)
ARS.End|$$ARR
' Test 2: FP Array Swap
ARS.New|$$FLT|FP
ARS.Add|$$FLT|1.1
ARS.Add|$$FLT|2.2
ARS.Add|$$FLT|3.3
DBP.Before swap: [1.1, 2.2, 3.3]
VAR.$$INA=2
VAR.$$INB=3
ARS.Swap|$$FLT|$$INA|$$INB
ARS.Get|$$FLT|2|$$VAL
DBP.After swap 2 and 3: $$VAL (Expected: 3.3)
ARS.Get|$$FLT|3|$$VAL
DBP. : $$VAL (Expected: 2.2)
ARS.End|$$FLT
' Test 3: String Array Swap
ARS.New|$$STR|String
ARS.Add|$$STR|Cat
ARS.Add|$$STR|Dog
ARS.Add|$$STR|Rat
DBP.Before swap: [Cat, Dog, Rat]
VAR.$$INA=1
VAR.$$INB=2
ARS.Swap|$$STR|$$INA|$$INB
ARS.Get|$$STR|1|$$VAL
DBP.After swap 1 and 2: $$VAL (Expected: Dog)
ARS.Get|$$STR|2|$$VAL
DBP. : $$VAL (Expected: Cat)
ARS.End|$$STR
' Test 4: Out-of-Range Index (Error Case)
ARS.New|$$ERR|Integer
ARS.Add|$$ERR|1
ARS.Add|$$ERR|2
DBP.Before swap: [1, 2]
VAR.$$INA=1
VAR.$$INB=5 ' Index beyond array size
ARS.Swap|$$ERR|$$INA|$$INB ' Should fail gracefully
DBP.(Expected: No crash, possible error message)
ARS.End|$$ERR
' Test 5: Same Index Swap (No Change Expected)
ARS.New|$$SAM|Integer
ARS.Add|$$SAM|10
ARS.Add|$$SAM|20
DBP.Before swap: [10, 20]
VAR.$$INA=1
VAR.$$INB=1
ARS.Swap|$$SAM|$$INA|$$INB
ARS.Get|$$SAM|1|$$VAL
DBP.After swap 1 and 1: $$VAL (Expected: 10, unchanged)
ARS.End|$$SAM
' End of Tests
DBP.All ARS.Swap tests completed
END.
Remarks
- Indices are 1-based, matching SPR array conventions.
- Swap occurs in-place within $$ARR.
Limitations:
- $$INA and $$INB must be within the array’s range.
- $$ARR must be initialized.
See also:
• ARS.New
• ARS.Add