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

I am studying Pascal and am currently facing the problem of manipulating arrays. I came across an array setup method that I saw in other languges, but I don't know how to do something like this in Pascal.

A variable declaration 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'm trying to set a multidimensional array directly, rather than scrolling it.

One of my main areas is to make it look like an image that is easy to read and understand.

Is there a way to achieve something like this?

I am using Borland Delphi 6 to compile a program.

+4
source share
3 answers

Delphi 6 does not have native support for initializing a dynamic array. For this, I would use a couple of helper functions:

 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 name 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]), ]); 
+3
source

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 dynamic array, you can write your own functions: Either line by line:

 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]); 
+2
source

Delphi 6 did not support any other means of initializing dynamic arrays except loops, so you were out of luck.

Later versions of Delphi support 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
source

Source: https://habr.com/ru/post/1479148/


All Articles