Array in array

BLM768 blm768 at gmail.com
Sat Oct 27 15:25:13 PDT 2012


On Saturday, 27 October 2012 at 21:16:56 UTC, xfiles wrote:
> Hi everybody!
> I want create a multi array like python.
> For example(in python):
> a=[1,2]
> a.append([1234],3)
> a.append([[4],5],6)
>
> and result is = [1,2,[1234],3,[[4],5],6]
>
> How can I do this in D

If you want to create it with one line, you can just write 
something like this:

int[] a = [1,2,[1234],3,[[4],5],6];

To build the array by appending to it, the ~= operator appends to 
an array in place:

int[] a = [1,2];
a ~= [[1234],3];
a ~= [[[4],5], 6];

You can also use the ~ operator:

a = [1,2,3];
a = a ~ [4, 5];
//a is now [1,2,3,4,5]

However, this is less efficient because it makes a copy of the 
original array.

The ~ and ~= operators can also append individual values:

a = [1];
a ~= 2;
a ~= 3;
//a is now [1,2,3]



More information about the Digitalmars-d mailing list