How to set multiple array fields at once without using a for loop in pascal?

StackOverflow https://stackoverflow.com/questions/16375584

  •  14-04-2022
  •  | 
  •  

Вопрос

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.

Это было полезно?

Решение

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]),
]);

Другие советы

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]);  

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);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top