Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: How do I access an array for which I only have a reference?

by le (Friar)
on Jun 07, 2000 at 00:59 UTC ( [id://16744]=note: print w/replies, xml ) Need Help??


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

$#{ $arrayref }
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.

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://16744]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-20 04:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found