Thread overview
Growing multidimensional dynamic arrays
Oct 08, 2012
KillerSponge
Oct 08, 2012
Ali Çehreli
Oct 08, 2012
KillerSponge
October 08, 2012
Hi all,

This seems like something that should be possible: how do I grow multidimensional arrays?

I want something like this:

struct X{ ... };
X*[][] listOfLists;
foreach ( x ; otherListOfX ) {
   if ( newListForArbitraryReason ) {
      listOfLists ~= new X*[];
   }

   listOfLists[$] ~= x;
}

Now, this doesn't compile, because I _have_ to give a size to new X*[](arbitrary_number), and the listOfLists[$] ~= x; line never works (hangs at runtime).

So, how would I go about doing this? My apologies if this is something really obvious.
October 08, 2012
On 10/08/2012 06:12 AM, KillerSponge wrote:
> Hi all,
>
> This seems like something that should be possible: how do I grow
> multidimensional arrays?
>
> I want something like this:
>
> struct X{ ... };
> X*[][] listOfLists;
> foreach ( x ; otherListOfX ) {
> if ( newListForArbitraryReason ) {
> listOfLists ~= new X*[];
> }
>
> listOfLists[$] ~= x;
> }
>
> Now, this doesn't compile, because I _have_ to give a size to new
> X*[](arbitrary_number), and the listOfLists[$] ~= x; line never works
> (hangs at runtime).
>
> So, how would I go about doing this? My apologies if this is something
> really obvious.

I don't see the need for 'new' nor the use of a pointer, so I will not use them (yet): :)

import std.stdio;

struct X
{
    int i;
}

void main()
{
    X[][] listOfLists;

    auto otherListOfX = [ X(1), X(2), X(3) ];
    auto newListForArbitraryReason = true;

    foreach (x; otherListOfX) {
        if (newListForArbitraryReason) {
            X[] newList = [ x ];
            listOfLists ~= newList;
        }
    }

    writeln(listOfLists);
}

The body of foreach can be shorter:

            listOfLists ~= [ x ];

Also note that

- There is no need for the semicolon at the end of the struct definition.

- $ is not a valid index value. The last element is indexed by $-1.

Ali

-- 
D Programming Language Tutorial: http://ddili.org/ders/d.en/index.html
October 08, 2012
On Monday, 8 October 2012 at 13:56:00 UTC, Ali Çehreli wrote:
> I don't see the need for 'new' nor the use of a pointer, so I will not use them (yet): :)
>
> import std.stdio;
>
> struct X
> {
>     int i;
> }
>
> void main()
> {
>     X[][] listOfLists;
>
>     auto otherListOfX = [ X(1), X(2), X(3) ];
>     auto newListForArbitraryReason = true;
>
>     foreach (x; otherListOfX) {
>         if (newListForArbitraryReason) {
>             X[] newList = [ x ];
>             listOfLists ~= newList;
>         }
>     }
>
>     writeln(listOfLists);
> }
>
> The body of foreach can be shorter:
>
>             listOfLists ~= [ x ];
>
> Also note that
>
> - There is no need for the semicolon at the end of the struct definition.
>
> - $ is not a valid index value. The last element is indexed by $-1.
>
> Ali

Ah, that works great (even with pointers ;)! Thanks a lot! :) It seems so obvious now.

And the use of $ was indeed a stupid mistake on my part. I guess I was confused because of the way it is often used in slicing. I really should pay attention to that :)