Delphi - How to assign data in a multidimensional array in a single command?

1

I'm trying to do the following but it's not working:

Var
  MyArray: array[1..3] of array[1..3] of Ttime;
Begin
   MyArray:=( ( StrToTime('08:25'), StrToTime('08:25'), StrToTime('08:50') ),
              ( StrToTime('09:25'), StrToTime('08:25'), StrToTime('08:25') ),
              ( StrToTime('10:25'), StrToTime('08:25'), StrToTime('08:25') )
            );
end;
    
asked by anonymous 20.06.2018 / 21:27

2 answers

2

I found a answer in SOen for the question Pass a multidimensional array as a parameter in Delphi

Converting to your case would look something like:

type
  TMatrix = array[1..3,1..3] of Ttime;
procedure  MakeMat(var c: TMatrix; nr, nc: integer; a: array of Ttime);
var
  i, j: integer;
begin
  //SetLength(c, nr, nc);

  for i := 0 to nr-1 do
    for j := 0 to nc-1 do
      c[i,j] := a[i*nc + j];
end;
Var
  MyArray: TMatrix;
Begin
  MakeMat(MyArray,3,3,[  StrToTime('08:25'), StrToTime('08:25'), StrToTime('08:50') ,
                         StrToTime('09:25'), StrToTime('08:25'), StrToTime('08:25') ,
                         StrToTime('10:25'), StrToTime('08:25'), StrToTime('08:25')
                      ]);

end;
    
20.06.2018 / 22:17
2

I suggest the following:

var
  MyArray: array of array of TTime;
begin
  SetLength(MyArray, 3, 3);

  MyArray := [[StrToTime('08:25'), StrToTime('08:25'), StrToTime('08:50')],
              [StrToTime('09:25'), StrToTime('08:25'), StrToTime('08:25')],
              [StrToTime('10:25'), StrToTime('08:25'), StrToTime('08:25')]];

You can explore the intrinsic functions that can help with array manipulation, especially Insert and Delete: link link link

It would look something like this:

var
  MyArray: array of array of TTime;
begin
  SetLength(MyArray, 3, 3);

  Insert([[StrToTime('08:25'), StrToTime('08:25'), StrToTime('08:50')],
          [StrToTime('09:25'), StrToTime('08:25'), StrToTime('08:25')],
          [StrToTime('10:25'), StrToTime('08:25'), StrToTime('08:25')]], MyArray, 1);

More about array: link

    
26.06.2018 / 20:17