Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: Accessing an AoHoAoH

by bart (Canon)
on Jun 06, 2004 at 16:36 UTC ( [id://361770]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Accessing an AoHoAoH
in thread Accessing an AoHoAoH

I guess I'm still a little confused about when to use parens and when to use brackets.
Parens build a list, brackets build an anonymous array, and return a reference to it. Hashes can only contain scalars as values, so a list won't ever do (except when it contains only one item — but then you don't need a list). An array reference is a scalar, so that will work. As it's the only thing that actually does work properly, Perl's syntax has been optimized to ease access to array references as hash and array values.

$x->{'foo'}[1] actually means: use $x as a hash reference, get the value associated with the string 'foo'. Use this as an array reference and access the second element (with index 1) from it.

Actually you can even split this up into:

$aref = $x->{'foo'}; $aref->[1]
provided you didn't need autovivification, which were to happen in case $x->{'foo'} was undefined, in the former case — but not in the latter.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-26 01:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found