|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > Arrays and Data-Structures > STK. - Stack > STK.Operations > Stack Operations |
MiniRobotLanguage (MRL)
STK.Swap|$$STK
Swap the top two elements on the stack.
Intention
The STK.Swap command exchanges the positions of the top two elements on the stack. This is useful for rearranging stack data for further operations.
If there are fewer than two elements on the stack, the command will result in an error.
Syntax
STK.Swap|$$STK
Parameter Explanation
P1 - $$STK: The stack handle where the top two elements are swapped.
Example
'***********************************
' Example 1: Swap Top Two Elements
'***********************************
STK.New|$$STK
STK.Push|$$STK|First
STK.Push|$$STK|Second
STK.Swap|$$STK
STK.Pop|$$STK|$$RES
JIS.$$RES!First|Lab_Failed
STK.Pop|$$STK|$$RES
JIS.$$RES!Second|Lab_Failed
STK.End|$$STK
ENR.
'***********************************
' Example 2: Swap Top Two Numeric Elements
'***********************************
STK.New|$$STK
STK.Push|$$STK|10
STK.Push|$$STK|20
STK.Swap|$$STK
STK.Pop|$$STK|$$RES
JIV.$$RES!10|Lab_Failed
STK.Pop|$$STK|$$RES
JIV.$$RES!20|Lab_Failed
STK.End|$$STK
ENR.
Remarks
- Ensure there are at least two elements on the stack before using this command to avoid errors.
Limitations:
- The stack handle must be valid before calling STK.Swap. Invalid handles will result in an error.
See also:
• STK.New
• STK.End