# dynamic 2D array

Questions about MultiCharts and user contributed studies.
kernel
Posts: 91
Joined: 19 Feb 2013
Has thanked: 21 times
Been thanked: 4 times

### dynamic 2D array

Hi,

Wonder would MC team consider adding dynamic 2D array alongside with its existing 1D sibling?
If it's already exist, excuse me for my ignorance then.

For example:

Code: Select all

``` 2DArrays: my2DArray1[ , ], my2DArray2[ , 2]; 2DArray_setmaxrows(my2DArray1, 10); 2DArray_setmaxcolumns(my2DArray1, 15); 2DArray_setmaxrows(my2DArray2, 5); ...... value1=2DArray_getmaxrows(my2DArray1); value2=2DArray_getmaxcolumns(my2DArray1); ...... ```

fbertram
Posts: 166
Joined: 16 Oct 2014
Location: Seattle, USA
Has thanked: 36 times
Been thanked: 73 times
Contact:

### Re: dynamic 2D array

Hi Kernel,

you can do this using the Lua integration I created. See here:
viewtopic.php?f=5&t=49242

The code for creating and using a dynamic 2D array would look like this:

Code: Select all

``` //============================================================================== // Description: Demo code to show dynamic 2D arrays with Lua // History: 2016i15, FUB, created //============================================================================== variables: LuaState(0); once begin ClearPrintLog; // create a new Lua environment LuaState = Lua.New(); // set the dimensions of the array Lua.SetGlobalNumber(LuaState, "ii", 30); Lua.SetGlobalNumber(LuaState, "jj", 25); // create a 1-based 2D array and initialize w/ zero Lua.Exec(LuaState, "A={} for i=1,ii do A[i]={} for j=1,jj do A[i][j]=0 end end"); // set values in 2D array for value1 = 1 to 30 begin for value2 = 1 to 25 begin Lua.SetGlobalNumber(LuaState, "x", value1); Lua.SetGlobalNumber(LuaState, "y", value2); Lua.SetGlobalNumber(LuaState, "z", value1 + value2); Lua.Exec(LuaState, "A[x][y]=z"); end; end; // retrieve values from 2D array for value1 = 1 to 30 begin for value2 = 1 to 25 begin Lua.SetGlobalNumber(LuaState, "x", value1); Lua.SetGlobalNumber(LuaState, "y", value2); Lua.Exec(LuaState, "z=A[x][y]"); print("A[", value1:1:0, "][", value2:1:0, "]=", Lua.GetGlobalNumber(LuaState, "z"):1:0); end; end; // close Lua environment Lua.Close(LuaState); end; //============================================================================== // end of file ```
Hope this helps,
best regards

Felix