Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
more useful options
 
PerlMonks  

hash within an array within a hash

by jwexfyfz (Initiate)
on Sep 06, 2012 at 22:33 UTC ( #992199=perlquestion: print w/ replies, xml ) Need Help??
jwexfyfz has asked for the wisdom of the Perl Monks concerning the following question:

If I have the given code:
# 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, );
How would I print "Santa Claus" (name of owner1Ref) by accessing it through account?

Comment on hash within an array within a hash
Download Code
Re: hash within an array within a hash
by Rudolf (Monk) on Sep 06, 2012 at 22:55 UTC

    Here is a step by step sort of thing I hope it helps decrypt it a bit ! :) Ill crunch it all into one thing too then.

    # drawn out my @anon = @{$account{'owners'}}; my %owner1 = %{$anon[0]}; my $name = $owner1{'name'}; print $name; ##### # one line print ${$account{'owners'}[0]}{'name'};
Re: hash within an array within a hash
by bergbrains (Acolyte) on Sep 07, 2012 at 01:18 UTC

    I think visually, so it helps to use Data::Dumper to get a visual display of what the data structure looks like:

    print Dumper( $account{owners} );

    Add this after your code and you get:

    $VAR1 = [ { 'DOB' => '1882-12-25', 'name' => 'Santa Claus' }, { 'DOB' => '1928-11-18', 'name' => 'Mickey Mouse' } ];
Re: hash within an array within a hash
by Kenosis (Priest) on Sep 07, 2012 at 01:48 UTC

    You have the following line in your script:

    my $ownersRef = [ $owner1Ref, $owner2Ref ];

    A two-element anonymous array is created, and $ownersRef is a reference to it. To get at the element 0 of this array, the arrow operator can be used to deference the array reference:

    print $ownersRef->[0]

    Output:

    HASH(0x27ee58)

    Element 0 in the anonymous array is a hash reference (and so is element 1). So, to get "Santa Claus" (sounds like a movie plot, doesn't it???!), another arrow operator can be used for dereferencing the hash reference:

    print $ownersRef->[0]->{name}; ^ ^ | | | + hash key for "Santa Claus" | + array element 0

    Output:

    Santa Claus
Re: hash within an array within a hash
by kcott (Abbot) on Sep 07, 2012 at 06:54 UTC
    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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2014-04-21 12:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (495 votes), past polls