An archive of community.esquilo.io as of Saturday January 26, 2019.

Two-dimensional Array Syntax

gary.vandergaast

Being somewhat new to Squirrel coding, and not finding any examples in documentation or Community questions, I would like to know how to set up a two-dimensionsl array in a NUT program, i.e., what is the syntax for doing the equivalent of the code snippet below, but using a single, two-dimensional array structure?

`local latest360I = array(360);
local latest360E = array(360);

for (local i = 0; i < 360; i++)
{
latest360I[i] = i;
latest360E[i] = i * 2.5;
}

`

robertjensen

Make each element of a one dimensional array another array.

local x = array(5);
for(local i=0;i<5;i++) x[i] = array(5);

for(local i=0;i<5;i++){
    for(local j=0;j<5;j++){
        x[i][j] = 10*i+j;
    }
}

for(local i=0;i<5;i++){
    for(local j=0;j<5;j++){
        print(format("%02d ",x[i][j]));
    }
    print("\n");
}

The output is

00 01 02 03 04 
10 11 12 13 14 
20 21 22 23 24 
30 31 32 33 34 
40 41 42 43 44
robertjensen

I could have answered the question more directly. Here is what was requested.

local latest360 = array(2);
latest360[0] = array(360);
latest360[1] = array(360);

for (local i = 0; i < 360; i++)
{
latest360[0][i] = i;
latest360[1][i] = i * 2.5;
}