Growing multidimensional dynamic arrays

KillerSponge killersponge at gmail.com
Mon Oct 8 06:58:14 PDT 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 :)


More information about the Digitalmars-d-learn mailing list