Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: hash within an array within a hash

by kcott (Chancellor)
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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://992254]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2017-07-22 03:57 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (336 votes). Check out past polls.