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

Asked

Viewed 555 times

1

I’m trying to do this 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;

2 answers

2

I found a reply in Soen concerning the question Pass the 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;
  • I mean, there is no way, can only abstract? Now I don’t know if I vote because I know if it’s true, I don’t know enough Delphi for that :(

  • Look I believe that there is no way, the usual way to set the array would be MyArray[1,2] := StrToTime('08:25'); but let’s wait if someone else presents another solution :)

  • From what I’ve seen, that’s right.

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 in array manipulation, especially Insert and Delete: http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Delphi_Intrinsic_Routines http://docwiki.embarcadero.com/Libraries/Tokyo/en/System.Insert http://docwiki.embarcadero.com/Libraries/Tokyo/en/System.Delete

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: http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Structured_Types_(Delphi)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.