4

I am learning Pascal and currently stuck with a problem concerning array manipulation. I have come across a method of setting arrays, that I have seen in other languges, but I do not know how to do something similar in Pascal.

The declaration of the variable looks something like this:

rotationBounds: array of array of integer;
setLength(rotationBounds, 5, 5);

And I want to do something like this:

 rotationBounds :=
         [
          [0, 0, 0, 0, 0],
          [0, 1, 1, 0, 0],
          [0, 0, 1, 0, 0],
          [0, 0, 1, 1, 0],
          [0, 0, 0, 0, 0],
         ]; 

Basically, I am trying to set a multi-dimentional array directly, rather than looping through it.

One of my focuses is to make it look like a picture, easy to read and understand.

Is there a way I could achieve something like this?

I am using Borland Delphi 6 to compiler the progam.

2
  • Not enough information. Is the array a variable, or a constant? Is it static or dynamic? What version of Pascal/Delphi/FPC? (Features supported vary.) Commented May 4, 2013 at 15:01
  • If you need a matrix, dynamic array of dynamic array is overkill, since type of all elements have to be the same and matrix has to be rectangular (with notable exception of sparse matrices and few of its applications) Commented May 4, 2013 at 21:37

3 Answers 3

3

In Delphi 6 there is no built in support for dynamic array initialization. I'd use a pair of helper functions for this:

type
  TIntegerArray = array of Integer;
  TIntegerMatrix = array of TIntegerArray;

function IntegerArray(const Values: array of Integer): TIntegerArray;
var
  i: Integer;
begin
  SetLength(Result, Length(Values));
  for i := 0 to high(Result) do
    Result[i] := Values[i];
end;

function IntegerMatrix(const Values: array of TIntegerArray): TIntegerMatrix;
var
  i: Integer;
begin
  SetLength(Result, Length(Values));
  for i := 0 to high(Result) do
    Result[i] := Values[i];
end;

And then call it like this:

var
  rotationBounds: TIntegerMatrix;
....
rotationBounds := IntegerMatrix([
  IntegerArray([0, 0, 0, 0, 0]),
  IntegerArray([0, 1, 1, 0, 0]),
  IntegerArray([0, 0, 1, 0, 0]),
  IntegerArray([0, 0, 1, 1, 0]),
  IntegerArray([0, 0, 0, 0, 0]),
]);
Sign up to request clarification or add additional context in comments.

1 Comment

IMHO this is the cleanest solution!
2

You can use it with ( in variable declarations

const temp: array[1..5, 1..5] of integer = (
   (0,0,0,0,0),
   (0,1,1,0,0),
   (0,0,1,0,0),
   (0,0,1,1,0),
   (0,0,0,0,0)
   );

and then

rotationBounds := temp;

If you have a dynamical array, you can write your own functions: Either row by row:

procedure setrow(var a: array of integer; b: array of integer);
begin
  if length(a) <> length(b) then raise Exception.Create('Invalid size');
  move(b[0], a[0], length(a) * sizeof(a[0]));
end;  

setrow(a[0], [0,0,0,0,0]);
setrow(a[1], [0,1,1,0,0]);
setrow(a[2], [0,0,1,0,0]);
setrow(a[3], [0,0,1,1,0]);
setrow(a[4], [0,0,0,0,0]);

Or all together:

type TIntArrayArray = array of array of integer;
procedure setall(var a: TIntArrayArray; b: array of integer);
var
  i: Integer;
begin
  if (length(a)*length(a[0]) <> length(b)) then raise Exception.Create('Invalid size');
  for i:= 0 to high(a) do
    move(b[i*length(a[0])], a[i,0], length(a[0]) * sizeof(a[0,0]));
end;              

setall(a, [0,0,0,0,0,
           0,1,1,0,0,
           0,0,1,0,0,
           0,0,1,1,0,
           0,0,0,0,0]);  

8 Comments

Can I use this method after the array has been created? It's a dynamic array of array of integer;
@Rasteril: what compiler are you using? In FPC trunk and recent Delphi you can use dynamic array constructor (wiki.freepascal.org/…)
I am using Borland Delphi 6, I am not sure what compiler that could be. Is there a way I could find out?
That would be the Borland Delphi 6 compiler. :-)
Using a statical array, or a one-dimensional array (with 25 elements) is usually more efficient. With the 2d array, you can just write your own functions. See edit
|
1

Delphi 6 didn't support any other means of initializing dynamic arrays other than looping, so you're out of luck.

More recent versions of Delphi support a constructor-type initialization:

type
  TIntArray = array of Integer;

var
  IntArray: TIntArray;
begin
  IntArray := TIntArray.Create(0, 0, 1, 1, 0, 0, 1, 0, 0);

1 Comment

That's a shame. Thank you for the clarification.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.