Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: hash within an array within a hash

by kcott (Abbot)
on Sep 07, 2012 at 06:54 UTC ( #992254=note: print w/ replies, xml ) Need Help??


in reply to hash within an array within a hash

How would I print "Santa Claus" (name of owner1Ref) by accessing it through account?
print $account{owners}[0]{name};

You don't need the additional syntax shown in earlier replies: nested braces; dereferencing (->); quoting.

Here's my test:

$ perl -Mstrict -Mwarnings -le ' # Braces denote an anonymous hash my $owner1Ref = { "name" => "Santa Claus", "DOB" => "1882-12-25", }; my $owner2Ref = { "name" => "Mickey Mouse", "DOB" => "1928-11-18", }; # Square brackets denote an anonymous array my $ownersRef = [ $owner1Ref, $owner2Ref ]; my %account = ( "number" => "12345678", "opened" => "2000-01-01", "owners" => $ownersRef, ); print $account{owners}[0]{name}; ' Santa Claus

See also: perldsc - Perl Data Structures Cookbook

-- Ken


Comment on Re: hash within an array within a hash
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (13)
As of 2015-07-28 23:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (260 votes), past polls