<< Click to Display Table of Contents >> Navigation: 3. Script Language > Variable Definitions and Operations > Array -Commands > Array -Commands |
MiniRobotLanguage (MRL)
Using ARR. to emulate 3D - Arrays
You can use up to 33 (0 to 32) 3D - Arrays using these Macros.
With the SPR, you declare a fixed-size array using the %Dim macro.
This macro specifies the name of the array and the dimensions it will contain.
For example:
' We dim a 3D-Array X(4,3,5) as String
' This Array cannot be resized automatically
%Dim 0|4|3|5
Here, "0" is the array name, and 4, 3, 5 are the dimensions along the X, Y, and Z axes, respectively.
Limitations of Fixed-Size Arrays
•No Dynamic Resizing
•Once you've declared the size of an array using %Dim, you can't change its size while the script is running. This is known as a "static" or "fixed-size" array.
Why Fixed-Size Arrays Cannot Be Resized
With the SPR, the size of an 3D-Array is determined at the time of its declaration, and the memory is allocated accordingly.
This makes array operations faster but also means you can't resize the array once it's been created.
Attempting to access an index outside the declared size will result in overwriting other Elements.
Remember that we project the 3 Dimensions here into a 1D-Array.
Working with Array Elements
To set or get elements in a fixed-size array, you can use the %SetElement and %GetElement macros. For example:
%SetElement 0|2|2|2|TextA
%GetElement 0|2|2|2|$$REA
Here, "0" is the array name, 2|2|2 are the indices along the X, Y, and Z axes, and TextA is the value you want to set at that index.
$$REA is the variable where the retrieved value will be stored.
Practical Applications
Fixed-size arrays are useful when you know in advance how many elements you'll need, and that number won't change.
They are faster and can be more memory-efficient than dynamic arrays but lack the flexibility to adapt to changing script requirements.
Summary
Fixed-size arrays with the SPR are a powerful tool for storing multiple values in a single data structure.
However, they come with the limitation that they cannot be resized at runtime.
Also here you can not mix data-types easily.
Here is a working Sample with a 3D-Array. We are projecting the 3 Dimensions into a 2D-Array.
' We dim an 3D-Array X(4,3,5) as String
' This Array can not be resized automatically
' P1 - Array-Name
' P2 - X-Index
' P3 - Y-Index
' P4 - Z-Index
%Dim 0|4|3|5
%SetElement 0|2|2|2|TextA
%SetElement 0|3|2|4|TextB
%GetElement 0|2|2|2|$$REA
%GetElement 0|3|2|4|$$REB
MBX. We got back: $$REA - $$REB
ENR.
'===========================================================
'-----------------------------------------------------------
'-----------------------------------------------------------
' MACROS
'-----------------------------------------------------------
' While Array-Names start at "0" we will only use 1 to 30 here for Simplicity
' P1 - Array Name
' P2 - P4 Dimensions
' We use global variables $$XSZ,$$YSZ and $$ZSZ for Staring the Size of each Dimension
: %Dim 4
#IF PARAMS=4
SAV.Save|$$LOA|$$LOB
$$LOA=§§§01
$$XSZ=§§§02
$$YSZ=§§§03
$$ZSZ=§§§04
CAL.$$SIZ=$$XSZ*$$YSZ*$$ZSZ-1
ARR.Dim Array|$$LOA|$$SIZ
#ELS
MBX.Too few Parameters.
END.
#EIF
SAV.Restore
END%
'-----------------------------------------------------------
'
'-----------------------------------------------------------
' P1 - Array Name
' P2 - P4 Dimensions
' P5 - Value to set
'
' Set Element P1 with fixed value P2
: %SetElement 5
#IF PARAMS=5
SAV.Save|$$LOX|$$LOY|$$LOZ
$$NAM=§§§01
$$LOX=§§§02
$$LOY=§§§03
$$LOZ=§§§04
CAL.$$IND=$$LOX*($$YSZ*$$ZSZ)+$$LOY*$$ZSZ+$$LOZ
ARR.Set|$$NAM|$$IND|§§§05
#ELS
MBX.Too few Parameters.
END.
#EIF
SAV.Restore
END%
'-----------------------------------------------------------
'
'-----------------------------------------------------------
' P1 - Array Name
' P2 - P4 Dimensions
' P5 - Value to get
'
' Set Element P1 with fixed value P2
: %GetElement 5
#IF PARAMS=5
SAV.Save|$$LOX|$$LOY|$$LOZ
$$NAM=§§§01
$$LOX=§§§02
$$LOY=§§§03
$$LOZ=§§§04
CAL.$$IND=$$LOX*($$YSZ*$$ZSZ)+$$LOY*$$ZSZ+$$LOZ
ARR.Get|$$NAM|$$IND|§§§05
#ELS
MBX.Too few Parameters.
END.
#EIF
SAV.Restore
END%
'-----------------------------------------------------------
'
'-----------------------------------------------------------