Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

hashes of hashes

by valavanp (Curate)
on Jul 15, 2006 at 12:36 UTC ( [id://561432]=perlquestion: print w/replies, xml ) Need Help??

valavanp has asked for the wisdom of the Perl Monks concerning the following question:

While using hashes of hashes, i am getting only the reference value instead of hash values. Can anyone tell me where i am wrong in the below code. Thanks Monks for your reply.

%people=( 'Jones'=>{'name'=>'Alison', 'age'=>15, 'pet'=>'dog',}, 'Smith'=>{'name'=>'Tom', 'age'=>34, 'pet'=>'cat',}); foreach $key(keys %people) { print "Key is $key", "value is %{$people{$key}}"; }
The output is
Key is smith value is #{HASH(0x2251d0)} Key is Jones value is %{HASH(0 +x2250c8)}

20060715 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: hashes of hashes
by sara2005 (Scribe) on Jul 15, 2006 at 12:47 UTC

    You might find the documentation on perldsc helpful to understand the HoH.

    foreach my $key( keys %people ) { print "\nKey is $key:\n"; foreach my $element ( keys %{$people{$key}} ) { print "\t$element", "= $people{$key}{$element}\n"; } }
Re: hashes of hashes
by Tux (Canon) on Jul 15, 2006 at 12:53 UTC
    my %people = ( Jones => { name => "Alison", age => 15, pet => "dog" }, Smith => { name => "Tom", age => 34, pet => "cat" }, ); foreach my $key (keys %people) { print "Key is $key", "value is @{[%{$people{$key}}]}"; }
    It is because % is not expanded in interpolation
    # perl -le'%h=(foo=>1,bar=>'dog');print"%h"' %h # perl -le'%h=(foo=>1,bar=>'dog');print"@{[%h]}"' bar dog foo 1 #
Re: hashes of hashes
by liverpole (Monsignor) on Jul 15, 2006 at 13:55 UTC
    Hi valavanp,

    In such situations, I find it helpful to use Data::Dumper to see what the data really looks like (if necessary), and then break the final dereference into multiple lines for clarity and simplicity, as well as ease-of-use for further operations:

    use Data::Dumper; %people=( 'Jones'=>{'name'=>'Alison', 'age'=>15, 'pet'=>'dog',}, 'Smith'=>{'name'=>'Tom', 'age'=>34, 'pet'=>'cat',}); foreach $person (keys %people) { my $person_attributes = $people{$person}; # Use Data::Dumper here to be *sure* of the data format # of the variable $person_attributes. # # printf "Person attributes: %s\n", Dumper(\$person_attributes); # Display the values ... my @attributes = values %$person_attributes; printf "Attributes: %s\n", join(",", @attributes); # ... or make use of one or more attributes ... my $age = $person_attributes->{'age'}; do_something_with_age($age); # Process this person's age # ... or process all of the attributes together do_something_with_this_person($person_attributes); }

Re: hashes of hashes
by davido (Cardinal) on Jul 15, 2006 at 16:11 UTC

    Hashes don't interpolate within quoted strings. You need to do something like this instead:

    foreach my $last_name ( keys %people ) { print "Last name is $last_name.\n\tDetails are:\n"; print map { "$_: " . $people->{$last_name}{$_} . "\n\t" } keys %{$people->{$last_name}}; print "\n"; }

    That's untested, so you may have to tinker with it to get your exact desired output, but it should help.


Re: hashes of hashes
by GrandFather (Saint) on Jul 15, 2006 at 18:45 UTC

    As pointed out by others you don't get anything that is generally very useful when you interpolate a hash reference into a string. However, you can copy hash elements to an array and an array interpolates nicely (for some value of nice):

    use strict; use warnings; my %people=( 'Jones'=>{'name'=>'Alison', 'age'=>15, 'pet'=>'dog',}, 'Smith'=>{'name'=>'Tom', 'age'=>34, 'pet'=>'cat',} ); for (keys %people) { my @values = %{$people{$_}}; print "@values\n"; }


    pet cat name Tom age 34 pet dog name Alison age 15

    DWIM is Perl's answer to Gödel
Re: hashes of hashes
by planetscape (Chancellor) on Jul 16, 2006 at 07:33 UTC

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://561432]
Approved by Corion
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (1)
As of 2024-04-15 19:23 GMT
Find Nodes?
    Voting Booth?

    No recent polls found