Indexing with variables - multidimensional arrays in GT Designer3 scripts

Indexing with variables - multidimensional arrays in GT Designer3 scripts

Info
This article describes how to index multidimensional arrays using variables in GT Designer3 scripts

Indexing an array with variables (e.g. testArray[ i , j , k ]) is achieved by applying an offset to the first element of the array

Dimension multipliers

   For a three-dimension array testArray[i, j, k] with the following sizes:
      First dimension size: a
      Second dimension size: b
      Third dimension size: c
            The multiplayer for the third dimension is 1
            The multiplayer for the second dimension is c
            The multiplayer for the first dimension is b*c
      Offset = 1*k + (c)*j + (b*c)*i

Practical example:
   For an array defined as newArray[0..16, 0..3, 0..7] and an index reference of newArray[i,j,k], the calculation uses the following dimension sizes:
      a = 17
      b = 4
      c = 8
    Offset = 1*k + 8*j + (4*8)*i


GT Designer3 project

To test this method, a sample three-dimensional array was mapped to devices starting at D200

First dimension: Index value stored in D0
Second dimension: Index value stored in D1
Third dimension: Index value stored in D2

The final offset, stored in D10, is determined by multiplying and summing the dimensions indices.

Alert
The value of D10 must be updated within a separate script
Alert
Multipliers 100, 10, 1 depend on the array dimensions.

With the offset value updated, the code can reference the array by its name (testArray) or via the associated start device (D200).

Notes
Example:
  testArray[ i , j , k ],  where i=2, j=7, k=4

  The offset (D10) is caculated as the sum of: 100*i + 10*j + 1*k
  D10 = 100*2 + 10*7 + 1*4 = 274

  testArray[0,0,0])[D10]  =>  testArray[0,0,0])[274]  =>  testArray[2,7,4]