|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > Arrays and Data-Structures > !ARM. - Multidimensional Dynamic Arrays > Array Operations > ARM. - Array Operations |
MiniRobotLanguage (MRL)
ARM.Reverse
Reverses the order of both rows and columns in a two-dimensional array, creating a full mirror image.
Intention
The ARM.Reverse command inverts the order of both rows and columns in a two-dimensional (2D) array, effectively creating a full mirror image of the grid, where the top becomes the bottom and left becomes right. This is a key command for data transformation in MiniRobotLanguage, enabling developers to flip grids for tasks like mirroring a game board (e.g., flipping a chessboard-like grid for symmetry or reflection), inverting pixel grids in image processing, or reversing matrix orientations for scientific computations. 2D arrays, visualized as a grid like a chessboard with rows and columns (e.g., [1,1] to [8,8]), are emulated within a one-dimensional structure in MRL, and ARM.Reverse provides a straightforward way to achieve this reflection, preserving the array’s handle, dimensions, and data type (strings, integers, or floating-point numbers). It’s particularly useful in applications requiring visual or logical inversion, such as games, graphics programming, or data analysis, where a mirrored grid enhances gameplay, adjusts visuals, or simplifies calculations. The operation reverses all rows (top to bottom) and all columns (left to right) simultaneously, as demonstrated in the examples.
Illustration:
↔️ 2D Array Reversal: A chessboard-like grid with handle $$ARR, originally 3x2 [A1, A2; B1, B2; C1, C2], is reversed to [C2, C1; B2, B1; A2, A1], creating a full mirror image.
🔑 Handle: $$ARR remains valid, with rows and columns fully reversed.
Syntax
ARM.Reverse|$$ARR
Parameter Explanation
P1 - $$ARR - (Variable, 5 characters max)
The handle of the 2D array to reverse. This variable, limited to 5 characters, must contain a valid handle created by ARM.New, and it remains valid after the operation, with rows and columns fully reversed.
Examples
'--------------------------------------------------------
' ARM.Reverse - Sample 1: Reversing a String Array (Game Board) for Full Mirror
'--------------------------------------------------------
ARM.New|$$ARR|s
ARM.Redim|$$ARR|3|2
' Set values
ARM.Set|$$ARR|1|1|A1
ARM.Set|$$ARR|1|2|A2
ARM.Set|$$ARR|2|1|B1
ARM.Set|$$ARR|2|2|B2
ARM.Set|$$ARR|3|1|C1
ARM.Set|$$ARR|3|2|C2
DBP.2D Array (chessboard-like) before reversing: [A1, A2; B1, B2; C1, C2] (3x2)
' Reverse both rows and columns
ARM.Reverse|$$ARR
DBP.2D Array after reversing: [C2, C1; B2, B1; A2, A1] (3x2, fully mirrored)
' Verify mirrored values
ARM.Get|$$ARR|1|1|$$VAL
JIV.$$VAL!C2|Lab_failed
ARM.Get|$$ARR|1|2|$$VAL
JIV.$$VAL!C1|Lab_failed
ARM.Get|$$ARR|2|1|$$VAL
JIV.$$VAL!B2|Lab_failed
ARM.Get|$$ARR|2|2|$$VAL
JIV.$$VAL!B1|Lab_failed
ARM.Get|$$ARR|3|1|$$VAL
JIV.$$VAL!A2|Lab_failed
ARM.Get|$$ARR|3|2|$$VAL
JIV.$$VAL!A1|Lab_failed
ARM.End|$$ARR
'
'--------------------------------------------------------
' ARM.Reverse - Sample 2: Reversing a Numeric Array (Matrix) for Symmetry
'--------------------------------------------------------
ARM.New|$$ARR|i
ARM.Redim|$$ARR|2|3
' Set values
ARM.Set|$$ARR|1|1|1
ARM.Set|$$ARR|1|2|2
ARM.Set|$$ARR|1|3|3
ARM.Set|$$ARR|2|1|4
ARM.Set|$$ARR|2|2|5
ARM.Set|$$ARR|2|3|6
DBP.2D Array (matrix) before reversing: [1, 2, 3; 6, 5, 4] (2x3)
' Reverse both rows and columns
ARM.Reverse|$$ARR
DBP.2D Array after reversing: [4, 5, 6; 3, 2, 1] (2x3, fully mirrored)
ARM.End|$$ARR
'
'--------------------------------------------------------
' ARM.Reverse - Sample 3: Reversing a Floating-Point Array (Pixel Grid) for Visual Mirror
'--------------------------------------------------------
ARM.New|$$ARR|f
ARM.Redim|$$ARR|3|2
' Set values
ARM.Set|$$ARR|1|1|1.5
ARM.Set|$$ARR|1|2|-2.7
ARM.Set|$$ARR|2|1|3.14
ARM.Set|$$ARR|2|2|0.5
ARM.Set|$$ARR|3|1|-1.8
ARM.Set|$$ARR|3|2|4.2
DBP.2D Array (pixel grid) before reversing: [1.5, -2.7; 3.14, 0.5; -1.8, 4.2] (3x2)
' Reverse both rows and columns
ARM.Reverse|$$ARR
DBP.2D Array after reversing: [4.2, -1.8; 0.5, 3.14; -2.7, 1.5] (3x2, fully mirrored)
ARM.End|$$ARR
'
Remarks
- The array handle must be stored in a variable with a maximum length of 5 characters (e.g., $$ARR, $$TMP) and must be valid (created by ARM.New) before using ARM.Reverse. Use ARM.Validate to ensure the handle is valid if there’s any doubt, and ARM.GetDims to verify dimensions before and after reversing.
- Reversing flips both rows (top to bottom) and columns (left to right) simultaneously, creating a full mirror image of the grid. This preserves the array’s data type (strings, integers, or floating-point) and dimensions but reorders all elements, as shown in the examples with verification steps using ARM.Get.
- The performance of reversing a 2D array in MRL, due to its emulation within a one-dimensional structure, may introduce moderate overhead, especially for large arrays, as it requires reorganizing all elements. However, this is typically acceptable for most practical use cases, such as mirroring game boards, pixel grids, or matrices, though frequent reversals in very large arrays should be minimized for performance optimization.
- This command is ideal for data transformation in applications like games (mirroring boards for symmetry), image processing (flipping images for visual effects), or scientific computing (reversing matrices for analysis or visualization).
Limitations
- The array handle variable must not exceed 5 characters and must be valid; attempting to reverse an invalid handle may cause runtime errors.
- Reversing changes the logical order of the grid but not its dimensions; ensure subsequent operations account for the mirrored structure, as shown in the examples with verification using ARM.Get.
- Reversing very large 2D arrays may impact performance due to the emulation and data reorganization in MRL, though this is typically manageable for most applications like game boards or small matrices. For large arrays, consider minimizing reversals or optimizing data handling if performance is critical.
- The command assumes the handle refers to a 2D array; using it on other data types or structures may lead to undefined behavior or errors.
See also:
• ARM.New