<< Click to Display Table of Contents >> Navigation: 3. Script Language > Variable Definitions and Operations > Array -Commands > Array -Commands |
MiniRobotLanguage (MRL)
Using the ARR.- Command and Macros to emulate a 2D-Array
You can use this technique to get a fast 2D-Array
Generally the ARR.-Arrays are 1-dimensional, therefore they are "Auto-Dim".
You can combine them to use 2-dimensional Arrays because the "Array-Name" in fact is also a number between 0 and 32.
You can combine Arrays, using Macros, to build yourself 2-Dimensional Array.
In these Macros, you can also Mix the 3 data-types (INT,FP and String).
You can - for example - use 1. dimension as INT-ARRAY and a second dimension a STRING-Array.
This will give you an indexed String-Array.
Using this simple and fast System, the first Dimension is limited to the number of available Arrays that is currently 0 to 32.
The second Dimension is not really limited unless it exceeds the 32-bit Memory.
If you need more you can use the System described below, then this Limitation does not apply.
Yet these Arrays are not more "Auto-Dim". You have to dimension them properly before use in the final size.
Here is an example for a 2-dimensional Array which uses only String Arrays.
In this case we have limit in the first Dimension that comes from the number of available Arrays.
' We dim an Array (3,45) as String
%Dim 3|45
%SetElement 1|2|TextA
%SetElement 2|3|TextB
%GetElement 1|2|$$REA
%GetElement 2|3|$$REB
MBX. We got back: $$REA - $$REB
ENR.
'===========================================================
'-----------------------------------------------------------
' MACROS for 2-dimensional Arrays
'-----------------------------------------------------------
' While Array-Names start at "0" we will only use 1 to 30 here for Simplicity
' P1 - Number of Dimensons ( 1 to 30)
' P2 - Number of Elements
: %Dim 1
#IF PARAMS=2
SAV.Save|$$LOA|$$LOB
$$LOA=§§§01
$$LOB=§§§02
FOR.$$LU2|1|$$LOA
ARR.Dim Array|$$LU2|$$LOB
NEX.
#ELS
MBX.Too few Parameters.
END.
#EIF
SAV.Restore
END%
'-----------------------------------------------------------
'
'-----------------------------------------------------------
' P1 - Dimension 1
' P2 - Dimension 2
' P3 - Value to set
'
' Set Element P1 with fixed value P2
: %SetElement 3
#IF PARAMS=3
SAV.Save|$$LOA|$$LOB
$$LOA=§§§01
$$LOB=§§§02
ARR.Set|$$LOA|$$LOB|§§§03
#ELS
MBX.Too few Parameters.
END.
#EIF
SAV.Restore
END%
'-----------------------------------------------------------
'
'-----------------------------------------------------------
' We dim a 2D-Array X(4,3) as String
' This Array can not be resized automatically
' P1 - Array-Name
' P2 - X-Index
' P3 - Y-Index
%Dim 0|4|3
' Set elements in the 2D array
%SetElement 0|2|2|TextA
%SetElement 0|3|2|TextB
' Get elements from the 2D array
%GetElement 0|2|2|$$REA
%GetElement 0|3|2|$$REB
' Display the retrieved elements
MBX. We got back: $$REA - $$REB
ENR.
'-----------------------------------------------------------
'
'-----------------------------------------------------------
: %Dim 3
#IF PARAMS=3
SAV.Save|$$LOA|$$LOB
$$LOA=§§§01
$$XSZ=§§§02
$$YSZ=§§§03
CAL.$$SIZ=$$XSZ*$$YSZ-1
ARR.Dim Array|$$LOA|$$SIZ
#ELS
MBX.Too few Parameters.
END.
#EIF
SAV.Restore
END%
'-----------------------------------------------------------
'
'-----------------------------------------------------------
: %SetElement 4
#IF PARAMS=4
SAV.Save|$$LOX|$$LOY
$$NAM=§§§01
$$LOX=§§§02
$$LOY=§§§03
CAL.$$IND=$$LOX*$$YSZ+$$LOY
ARR.Set|$$NAM|$$IND|§§§04
#ELS
MBX.Too few Parameters.
END.
#EIF
SAV.Restore
END%
'-----------------------------------------------------------
'
'-----------------------------------------------------------
: %GetElement 4
#IF PARAMS=4
SAV.Save|$$LOX|$$LOY
$$NAM=§§§01
$$LOX=§§§02
$$LOY=§§§03
CAL.$$IND=$$LOX*$$YSZ+$$LOY
ARR.Get|$$NAM|$$IND|§§§04
#ELS
MBX.Too few Parameters.
END.
#EIF
SAV.Restore
END%
'-----------------------------------------------------------
'
'-----------------------------------------------------------