www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Growing multidimensional dynamic arrays

reply "KillerSponge" <killersponge gmail.com> writes:
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.
Oct 08 2012
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Oct 08 2012
prev sibling parent "KillerSponge" <killersponge gmail.com> writes:
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 :)
Oct 08 2012