<< 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 Smart Package Robot, 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 4D-Array $$ARN(4,3,2,5) as String
' This Array cannot be resized automatically
%Dim $$ARN|4|3|2|5
Here, "$$ARN" is the array name/number, and 4, 3, 2, 5 are the dimensions along the X, Y, Z, and W axes, respectively.
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 a 4D-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 may result in overwriting other elements or call the Auto-Dim which will destroy the order of the elements in the array.
Remember that we project the 4 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 $$ARN|1|1|1|1|$$TX0
%GetElement $$ARN|1|1|1|1|$$REA
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.
Here is a working sample using a 4D-Array. We are projecting the 4 dimensions into a 1D-Array.
' Declare a 4D Array with dimensions 4x3x2x5
' We use Array Nr.0 here
VIN.$$ARN=0
%Dim $$ARN|4|3|2|5
' Set some elements in the array
%SetElement $$ARN|1|1|1|1|$$TX0
%SetElement $$ARN|2|2|1|1|$$TX1
%SetElement $$ARN|3|2|2|3|$$TX2
%SetElement $$ARN|4|3|2|4|$$TX3
%GetElement $$ARN|1|1|1|1|$$TX4
%GetElement $$ARN|2|2|1|1|$$TX5
%GetElement $$ARN|3|2|2|3|$$TX6
%GetElement $$ARN|4|3|2|4|$$TX7
' Display the retrieved values
MBX. We got back: $$TX4 - $$TX5 - $$TX6 - $$TX7
END.
'-----------------------------------------------------------
' MACROS
'-----------------------------------------------------------
: %Dim 5
#IF PARAMS=5
SAV.Save|$$LOA|$$LOB|$$LOC|$$LOD
$$LOA=§§§01
$$XSZ=§§§02
$$YSZ=§§§03
$$ZSZ=§§§04
$$WSZ=§§§05
CAL.$$SIZ=$$XSZ*$$YSZ*$$ZSZ*$$WSZ-1
ARR.Dim Array|$$LOA|$$SIZ
#ELS
MBX.Too few Parameters.
END.
#EIF
SAV.Restore
END%
'-----------------------------------------------------------
'
'-----------------------------------------------------------
: %SetElement 6
#IF PARAMS=6
SAV.Save|$$LOX|$$LOY|$$LOZ|$$LOW
$$NAM=§§§01
$$LOX=§§§02
$$LOY=§§§03
$$LOZ=§§§04
$$LOW=§§§05
CAL.$$IND=$$LOX*($$YSZ*$$ZSZ*$$WSZ)+$$LOY*($$ZSZ*$$WSZ)+$$LOZ*$$WSZ+$$LOW
ARR.Set|$$NAM|$$IND|§§§06
#ELS
MBX.Too few Parameters.
END.
#EIF
SAV.Restore
END%
'-----------------------------------------------------------
'
'-----------------------------------------------------------
: %GetElement 6
#IF PARAMS=6
SAV.Save|$$LOX|$$LOY|$$LOZ|$$LOW
$$NAM=§§§01
$$LOX=§§§02
$$LOY=§§§03
$$LOZ=§§§04
$$LOW=§§§05
CAL.$$IND=$$LOX*($$YSZ*$$ZSZ*$$WSZ)+$$LOY*($$ZSZ*$$WSZ)+$$LOZ*$$WSZ+$$LOW
ARR.Get|$$NAM|$$IND|§§§06
#ELS
MBX.Too few Parameters.
END.
#EIF
SAV.Restore
END%
'-----------------------------------------------------------
'
'-----------------------------------------------------------
'-----------------------------------------------------------
'
'-----------------------------------------------------------