oldtechaa oldtechaa - 2 years ago 49
Perl Question

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

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?

Answer Source

You need to dereference the inner array:

@{ $arr[$x][$y] }[ 0 .. 2 ] = (0, 1, 2);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download