Cloning multidimensional arrays

What you have is not a multi-dimensional array, but rather an array of arrays. Since clone is shallow, it will just copy the top-level array. In this case, the clone is also redundant, since assignment into an array is already a copying operation.

A simple fix is to clone each of the nested arrays:

my @b = @a.map(*.clone);

Alternatively, you could use a real multi-dimensional array. The declaration would look like this:

my @a[3;3] = [0, 0, 0], [0, 0, 0], [0, 0, 0];

And then the copying into another array would be:

my @b[3;3] = @a;

The assignments also need updating to use the multi-dimensional syntax:

@a[0;1] = 1;
@b[1;0] = 1;

And finally, the result of this:

say '@a : ' ~ @a.gist;
say '@b : ' ~ @b.gist;

Is as desired:

@a : [[0 1 0] [0 0 0] [0 0 0]]
@b : [[0 0 0] [1 0 0] [0 0 0]]

As a final cleanup, you can also "pour" an conceptually infinite sequence of 0s into the array to initialize it:

my @a[3;3] Z= 0 xx *;

Which means that the 3x3 structure does not need to be replicated on the right.


@a and @b are not bound. They just happen to contain the same things. The clone does not recurse and only clones the outer Array.

One way to achieve what you want would be

@b = @a.map: *.clone; 

Tags:

Arrays

Clone

Raku