Results 1 to 8 of 8

Thread: Best way to prepopulate 2D array?

  1. #1
    Join Date
    Aug 2009
    Location
    Nova Scotia, Canada
    Posts
    604
    Mentioned
    0 Post(s)
    Quoted
    56 Post(s)

    Default Best way to prepopulate 2D array?

    What is the best/least painful way to prepopulate a 2 dimensional integer array?

    This compiles, but generates a type mismatch at runtime:
    Simba Code:
    program new;

    var
      IA: T2DIntegerArray;

    begin
      IA := [
        [0, 1, 2],
        [3, 4, 5]
      ];
    end.
    This works, but could get awkward with long lists:
    Simba Code:
    program new;

    var
      IA: T2DIntegerArray;

    begin
      SetLength(IA, 2);
      IA[0] := [0, 1, 2];
      IA[1] := [3, 4, 5];
    end.
    Toyed with the idea of just one huge integer array, given that the subarray has a fixed length.

    Also considered stuffing each subarray into a string and store in TStringArray, which could be further condensed with Implode, but there is no good routine to put the integer array into a string or tstringarray.

    There has to be a good way to do this?
    Never ever approach a computer saying or even thinking "I will just do this quickly".

  2. #2
    Join Date
    Oct 2007
    Location
    #srl
    Posts
    6,102
    Mentioned
    39 Post(s)
    Quoted
    62 Post(s)

    Default

    You have to declare the array like so:
    Simba Code:
    begin
      intArray := [TIntegerArray([1, 2, 3]), TIntegerArray([4, 5, 6])];
    end;

  3. #3
    Join Date
    Nov 2010
    Location
    Australia
    Posts
    1,472
    Mentioned
    0 Post(s)
    Quoted
    8 Post(s)

    Default

    use a loop to do it:

    Simba Code:
    program new;
    var
      a, b, num: Integer;
      IA: T2DIntegerArray;
    begin
      SetLength(IA, 3);
      SetLength(IA[0], 3);
      SetLength(IA[1], 3);
      SetLength(IA[2], 3);
      for a:= 0 to 2 do
        for b:= 0 to 2 do
        begin
          IA[a][b] := num;
          Inc(num);
        end;
      writeln(IA);
    end.

  4. #4
    Join Date
    Dec 2006
    Location
    Houston, TX USA
    Posts
    4,791
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    I'm interested to know what you are using this T2DIntegerArray for.

  5. #5
    Join Date
    Feb 2006
    Location
    Amsterdam
    Posts
    13,692
    Mentioned
    146 Post(s)
    Quoted
    130 Post(s)

    Default

    Quote Originally Posted by Bixby Sayz View Post
    What is the best/least painful way to prepopulate a 2 dimensional integer array?

    This compiles, but generates a type mismatch at runtime:
    Simba Code:
    program new;

    var
      IA: T2DIntegerArray;

    begin
      IA := [
        [0, 1, 2],
        [3, 4, 5]
      ];
    end.
    This works, but could get awkward with long lists:
    Simba Code:
    program new;

    var
      IA: T2DIntegerArray;

    begin
      SetLength(IA, 2);
      IA[0] := [0, 1, 2];
      IA[1] := [3, 4, 5];
    end.
    Toyed with the idea of just one huge integer array, given that the subarray has a fixed length.

    Also considered stuffing each subarray into a string and store in TStringArray, which could be further condensed with Implode, but there is no good routine to put the integer array into a string or tstringarray.

    There has to be a good way to do this?
    No promises, but this should work:

    Simba Code:
    IA := [
        TIntegerArray([0, 1, 2]),
        TIntegerArray([3, 4, 5])
      ];



    The best way to contact me is by email, which you can find on my website: http://wizzup.org
    I also get email notifications of private messages, though.

    Simba (on Twitter | Group on Villavu | Website | Stable/Unstable releases
    Documentation | Source | Simba Bug Tracker on Github and Villavu )


    My (Blog | Website)

  6. #6
    Join Date
    Aug 2009
    Location
    Nova Scotia, Canada
    Posts
    604
    Mentioned
    0 Post(s)
    Quoted
    56 Post(s)

    Default

    Quote Originally Posted by Wizzup? View Post
    No promises, but this should work:

    Simba Code:
    IA := [
        TIntegerArray([0, 1, 2]),
        TIntegerArray([3, 4, 5])
      ];
    Perfect. This is what I was looking for.
    Never ever approach a computer saying or even thinking "I will just do this quickly".

  7. #7
    Join Date
    Oct 2007
    Location
    #srl
    Posts
    6,102
    Mentioned
    39 Post(s)
    Quoted
    62 Post(s)

    Default

    Quote Originally Posted by Bixby Sayz View Post
    Perfect. This is what I was looking for.
    Lol, check the first reply.

  8. #8
    Join Date
    Aug 2009
    Location
    Nova Scotia, Canada
    Posts
    604
    Mentioned
    0 Post(s)
    Quoted
    56 Post(s)

    Default

    Quote Originally Posted by NCDS View Post
    Lol, check the first reply.
    I was just too lazy to quote both responses in my reply.
    Never ever approach a computer saying or even thinking "I will just do this quickly".

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •