Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning to a slice of a 3D array using the range operator

Tags:

arrays

slice

perl

I have a 3 dimensional array. I want to set three elements of it like this:

$array[$x][$y][0 .. 2] = (0, 1, 2);

but perl tells me:

Useless use of a constant (1) in void context

In array context:

@array[$x][$y][0 .. 2] = (0, 1, 2);

but perl tells me:

syntax error near "]["

presumably meaning that it expects me to give it two indices and then assign to the third dimension as a separate array? However, on this page, under Example: Assignment Using Array Slices, it suggests that it is possible to assign to a slice using the range operator where it says:

@array1[1..3] = @array2[23..25];

How can I assign to a slice of the array like this, or do I have to assign each index individually?

like image 467
oldtechaa Avatar asked Dec 25 '22 05:12

oldtechaa


1 Answers

You need to dereference the inner array:

@{ $arr[$x][$y] }[ 0 .. 2 ] = (0, 1, 2);
like image 96
choroba Avatar answered Dec 27 '22 12:12

choroba