How to create 2 dimensinal array and initialize it


shimh

Recommended Posts

I would like to create a 2 dimenstional string array and initialize it in a sequence. I found it is kinda difficult. I tried the following in the code block below:

arrSensorAssignment[x]={"No","n/a","n/a","2000","2000"}

The array come out is really 1 dimensional, which is {"No","No","No","No","No","n/a","n/a","2000","2000"}

I expect something like the following code:

global string arrSensorAssignment
Define MaxSensors = 5 

for (x = 0, x < MaxSensors, x++)
   arrSensorAssignment[x][0,4]={"No","n/a","n/a","2000","2000"}  //just to illustrate what I want to do
endfor

How to do it properly? Thanks.

Link to comment
Share on other sites

The problem is that using {...} is in the first dimension. You need two {{ to indicate the second dimension:

global string arrSensorAssignment
Define MaxSensors = 5

for (x = 0, x < MaxSensors, x++)
   arrSensorAssignment[x]={{"No","n/a","n/a","2000","2000"}}  
endfor

Notice also that you don't need to specify where in the second dimension you want it (i.e. no [0,4]). You can if you wanted to insert it other than the first column, but you'd only need a single specifier: arrSensorAssignment[x][3] for example to start in the 3rd column, xth row.

So:

{1,2,3}

is an array with 1 column, 3 rows

{{1,2,3}}

is an array with 3 columns, 1 row

{{1,2,3},{4,5,6},{7,8,9}}

is an array with 3 columns and 3 rows

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.