in reply to Changling here?
Quite simply, because array slices are not arrays.
Consider the equivalent using a regular array instead of a ref:
Even though we know that the element at $states[1] is an array, it doesn't matter; the slice is returning the single-element list ([]), and thus the scalar $aray_size receives the last element thereof - the (second, inner) arrayref.my @states = ( [], [] ); my $aray_size = @states[1];
What you need to do is make sure you've actually got the array-ref which is the second element of the (outer) array, dereference that, and subject that to the scalar() operator. Something like
(Note that the scalar op is implicit, due to the context transmitted by the assignment operator from the scalar variable on the LHS.)my $aray_size = @{ ${$states}[ 1 ] }; # or my $aray_size = @{ $states->[ 1 ] };
|
|---|