PreviousNext
Help > UNITS > Structural Arrays
Structural Arrays

Units can be combined in arrays just like the other single data types. A unit structure can be declared as a single variable array just like any normal variable:

unit something[10];
      var int member1;
      var text member2 maxlen [30];
endunit;

 

The example above creates a 10-element array of units with common name “something”. The access to individual elements in a unit has a standard format:

unit_name . member [ . member … ] [ index [: index …] ]

In an example with the unit from above:

something.member1[3] will refer to the variable “member1” in the fourth unit structure (arrays always start from index 0).

When a unit is used as data type for another variable, which is array, then the number of dimensions are getting increased accordingly. For example:

var something newer[15];

This will crate a unit structure “newer” built from unit structures “something”. But in the previous example “something” is actually an array of 10 elements, so the new variable “newer” will inherit the original dimension, and add its own on top of it. As result the new variable will have two dimensions: [10] inherited from “something” and [15] as declared in “newer”. Therefore accessing elements will require two indexes:

newer.member1[3:7]

In case internal member variables in a unit also have their own dimensions, those are added as junior level indexes when accessing the member variables:

Let’s consider this unit:

unit pack;
      var int ival[12];
endunit;

var pack p[20];

The newly defined variable is built from 20 units of type “pack”, each one of which containing a single member array “ival[12]”.

Accessing “p” would look like this: p.ival [p_index : ival_index]

Generally speaking indexes are always added by seniority of declaration – units first, then member units (if any), top-level indexes, and finally individual indexes.

Working with units containing too many indexes may become very confusing, so as a rule of thumb it is always good to optimise and keep the number of needed dimensions as low as possible. Internally the RVM always convers all multi-dimensional arrays to a single-dimension flat array, so from developer’s perspective maintaining a close proximity to how data is actually being stored in memory can be only beneficial for the reliability of the program.