Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 6 of 6 total
Thread Array initialization question
Fri, Oct 27 2017 7:24 AMPermanent Link

Michael Dreher

I have defined the following types and variables:

type
 TIntArray = array of integer;
 T2DIntArray = array of TIntArray;

var
 i : TIntArray;
 i2d : T2DIntArray;
 intValue : integer;

Here is a part of a procedure body:

procedure foo;
begin
 // ....
 i2d[0][0] := 42;
 intValue := i2d[0][0];
end;

While this all compiles, it will, of course, result in a "runtime error: 0 reference access access" because
the dymanic array yet is not initialized. But how to initialize a multidimensional array?

What I've unsuccessfully tried:

i) SetLength(i2d, 2);
This compiles but of course the runtime error remains(only one dimension is initialized).

ii) SetLength(i2d[0], 3);
Try to initialize the 2'nd dimension of the 1'st element, Compiler error: Expected parameter name or variable name but instead found i2d[0]

iii) SetLength(i2d, 2, 3);
Try to initialize an 2x3 array -> Compile error: "There is no function or procedure declaration that matches the SetLength(i2d, 2, 3) reference"

iv) i2d := [[1,2,3],[4,5,6]];
Try to initialize with const int values, Compiler error: "Expected T2DIntArray but instead found"

Thanks for replies.
Michael Dreher
Fri, Oct 27 2017 8:32 AMPermanent Link

Walter Matte

Tactical Business Corporation

Fri, Oct 27 2017 9:00 AMPermanent Link

Michael Dreher

Walter Matte wrote:

See: https://www [....]

The people in the thread above are talking about mimicking a 2D array with an 1D array and an index mapping. But I've declared a "real" 2D type. I think it's not the same.

M. Dreher
Fri, Oct 27 2017 9:38 AMPermanent Link

Walter Matte

Tactical Business Corporation

Since Javascript does not support multi dim arrays - so I don;t thing EWB does either.


From:  http://www.javascripter.net/faq/twodimensional.htm

Question: How do I create a two-dimensional array in JavaScript?

Answer: JavaScript does not have a special syntax for creating multidimensional arrays. A common workaround is to create an array of arrays in nested loops. (This technique is used, for example, to define the game board arrays in several games on this site, such as the JavaScript Tetris game.)

The following code example illustrates the array-of-arrays technique. First, this code creates an array f. Then, in the outer for loop, each element of f is itself initialized as new Array(); thus f becomes an array of arrays. In the inner for loop, all elements f[i][j] in each newly created "inner" array are set to zero.

var iMax = 20;
var jMax = 10;
var f = new Array();

for (i=0;i<iMax;i++) {
f[i]=new Array();
for (j=0;j<jMax;j++) {
 f[i][j]=0;
}
}
Fri, Oct 27 2017 10:01 AMPermanent Link

Mark Brooks

Slikware

Avatar

I've always handled this with a class that encapsulates the "2 dimensions" and provides accessor properties accordingly. For example, with a matrix class, I'd support properties like:

- XCount: integer;
- YCount: integer;
- Cells[X: integer; Y: integer]

Modifying XCount or YCount handles the storage resizing behind the scenes. The Cells property allows reading / writing to specific locations in the matrix.

Maybe over-complex for your needs though?

Mark (Keanu)
Fri, Oct 27 2017 10:51 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Michael,

<< ii) SetLength(i2d[0], 3);
Try to initialize the 2'nd dimension of the 1'st element, Compiler error: Expected parameter name or variable name but instead found i2d[0] >>

This is the bug - this should work.  I'll have a fix for this as soon as possible.

Tim Young
Elevate Software
www.elevatesoft.com
Image