SRL Archive - OSR Help

Best way to prepopulate 2D array?


Bixby Sayz

2011-11-06 23:10:33

What is the best/least painful way to prepopulate a 2 dimensional integer array?

This compiles, but generates a type mismatch at runtime:
program new;

var
IA: T2DIntegerArray;

begin
IA := [
[0, 1, 2],
[3, 4, 5]
];
end.
This works, but could get awkward with long lists:
program new;

var
IA: T2DIntegerArray;

begin
SetLength(IA, 2);
IA[0] := [0, 1, 2];
IA[1] := [3, 4, 5];
end.
Toyed with the idea of just one huge integer array, given that the subarray has a fixed length.

Also considered stuffing each subarray into a string and store in TStringArray, which could be further condensed with Implode, but there is no good routine to put the integer array into a string or tstringarray.

There has to be a good way to do this?

NCDS

2011-11-06 23:14:10

You have to declare the array like so:

begin
intArray := [TIntegerArray([1, 2, 3]), TIntegerArray([4, 5, 6])];
end;

KingKong

2011-11-06 23:50:02

use a loop to do it:


program new;
var
a, b, num: Integer;
IA: T2DIntegerArray;
begin
SetLength(IA, 3);
SetLength(IA[0], 3);
SetLength(IA[1], 3);
SetLength(IA[2], 3);
for a:= 0 to 2 do
for b:= 0 to 2 do
begin
IA[a][b] := num;
Inc(num);
end;
writeln(IA);
end.

Wanted

2011-11-07 00:48:23

I'm interested to know what you are using this T2DIntegerArray for.

Wizzup?

2011-11-07 06:32:57

From: Bixby Sayz
What is the best/least painful way to prepopulate a 2 dimensional integer array?

This compiles, but generates a type mismatch at runtime:
program new;

var
IA: T2DIntegerArray;

begin
IA := [
[0, 1, 2],
[3, 4, 5]
];
end.
This works, but could get awkward with long lists:
program new;

var
IA: T2DIntegerArray;

begin
SetLength(IA, 2);
IA[0] := [0, 1, 2];
IA[1] := [3, 4, 5];
end.
Toyed with the idea of just one huge integer array, given that the subarray has a fixed length.

Also considered stuffing each subarray into a string and store in TStringArray, which could be further condensed with Implode, but there is no good routine to put the integer array into a string or tstringarray.

There has to be a good way to do this?


No promises, but this should work:


IA := [
TIntegerArray([0, 1, 2]),
TIntegerArray([3, 4, 5])
];

Bixby Sayz

2011-11-07 06:57:47

From: Wizzup?
No promises, but this should work:


IA := [
TIntegerArray([0, 1, 2]),
TIntegerArray([3, 4, 5])
];


Perfect. This is what I was looking for.

NCDS

2011-11-07 08:28:54

From: Bixby Sayz
Perfect. This is what I was looking for.


Lol, check the first reply.

Bixby Sayz

2011-11-07 10:43:48

From: NCDS
Lol, check the first reply.

I was just too lazy to quote both responses in my reply. :)