⭐ in reply to How do I access an array for which I only have a reference?
In places where you want the index of the last element (e.g. in foreach loops), you'd use the syntax
It's just like the usual $#array, but with an array reference value in place of the array variable name. And you need the curly braces.$#{ $arrayref }
So, to iterate over the elements of an array to which you have a reference, using an index counter:
for ( my $i = 0; $i <= $#{ $arrayref }; $i++ ) { print "$arrayref->[$i] \n"; }
I think this is useful when you have arrays of arrays, or even more complex data structures.
|
|---|