Voyager + C++ With Multi-Dimensional Arrays (Part 1: Reading)
October 31st, 2010This is part 1 of this subject. Part 2 will be about writing values to the arrays.
One of the cool things with the latest version of Cube Voyager is multi-dimensional arrays. However, it appears behind the scenes (or at least to C++) that the multi-dimensional arrays are a wrapper over a single-dimension array.
The easiest way to show this is to make a random array and send it to the print file. Making the random array in Cube is simple:
RUN PGM=MATRIX PRNFILE="C:\TEMP\DTMAT00B.PRN" FILEO PRINTO[1] = "C:\TEMP\DEBUG.PRN" PAR ZONES=1 ARRAY MDARRAY=5,5 LOOP _c=1,5 LOOP _r=1,5 MDARRAY[_c][_r]=RAND() PRINT PRINTO=1 LIST='MDARRAY[',_c(1.0),'][',_r(1.0),']=',MDARRAY[_c][_r](8.6) ENDLOOP ENDLOOP CALL DLL=DLLFILE(TableReader) ENDRUN
Then, in C++ we can pull 25 (!) array values from this:
int TableReader (Callstack* Stack){ double* TableRecord; char message[100]; TableRecord=(double*)Stack->pfFindVar("MDARRAY",0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 16,17,18,19,20,21,22,23,24); for(int x=0;x<=24;x++){ if(&TableRecord!=0){ sprintf(message,"TableRecord=%f",TableRecord[x]); Stack->pfPrnLine(1,message); } } return 0; }
For fixed size multi-dimensional arrays, this isn’t really an issue. It would be very easy to wrap the Stack->pfFindVar line in a set of loops that fills a multi-dimensional array.