Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
go ahead... be a heretic
 
PerlMonks  

Accessing a Hash of Hashes

by George_Sherston (Vicar)
on Nov 17, 2001 at 01:04 UTC ( #125917=perlquestion: print w/ replies, xml ) Need Help??
George_Sherston has asked for the wisdom of the Perl Monks concerning the following question:

Siblings, I've got a nasty feeling this is going to be one of those nodes that stands as a lasting monument to my inability to see what's in front of my nose. But. I've got a data structure that prints out like this from Data::Dumper (just to be sure it really is the structure I want):
$VAR1 = { '1' => { '1' => { 'EMail' => 'norbert@thnwb.com', 'Name' => 'Norbert Bodkin' }, '5' => { 'EMail' => 'sue@thnwb.com', 'Name' => 'Sue Soup' } }, '2' => { '3' => { 'EMail' => 'max@thnwb.com', 'Name' => 'Max Grappler' } } };
Now, say I want to print out everyone's name, ordered first by the key of the "outer" hash and then by the next hash in (i.e. in this case exactly the order you see them above) I tried to do:
106 my $status; 107 for $status (keys %Status) { 108 for (keys %{ $Status{$status} }) { 109 print $Status{$status}{$_}{'Name'}, br; 110 } 111 }
(line numbers added for clarity) BUT... I get Can't use string ("1") as a HASH ref while "strict refs" in use at /home/htdocs/hosted/thinweb/lib/devbox.pm line 108.
I tried a few other lame ways to do it which I won't go into her, but when I RTFMed, I found that my first cut was pretty much the staff solution... as far as I could tell. I even got superstitious and tried
105 my $status; 106 my $id; 107 for $status (keys %Status) { 108 for $id (keys %{ $Status{$status} }) { 109 print $Status{$status}{$id}{'Name'}, br; 110 } 111 }
... nada.

I'm stumped... I crave enlightenment... no matter how humiliating.

George Sherston

Comment on Accessing a Hash of Hashes
Select or Download Code
Re: Accessing a Hash of Hashes
by c-era (Curate) on Nov 17, 2001 at 01:18 UTC
    Your problem is how you copy the data from $VAR1 to %Status. This code works for me:
    $VAR1 = { '1' => { '1' => { 'EMail' => 'norbert@thnwb.com', 'Name' => 'Norbert Bodkin' }, '5' => { 'EMail' => 'sue@thnwb.com', 'Name' => 'Sue Soup' } }, '2' => { '3' => { 'EMail' => 'max@thnwb.com', 'Name' => 'Max Grappler' } } }; my %Status = %$VAR1; my $status; for $status (keys %Status) { for (keys %{ $Status{$status} }) { print $Status{$status}{$_}{'Name'}, br; } }
    The error message tells you, that instead of a hashref you have a value. If you post the code that copies $VAR1 to %Status, I would be able to help you further.

    Also, hashes don't have order. So, if you want to do a sort on keys, you'll need to create a sorted array with those keys.

Re: Accessing a Hash of Hashes
by CubicSpline (Friar) on Nov 17, 2001 at 01:22 UTC
    This worked out just fine for me, it's your original code:

    106 my $status; 107 for $status (keys %Status) { 108 for (keys %{ $Status{$status} }) { 109 print $Status{$status}{$_}{'Name'}, br; 110 } 111 }

    I'm curious how you declared your HoHoH. I took your example above and declared it like this:

    %h = ( '1' => { '1' => { 'EMail' => 'norbert@thnwb.com', 'Name' => 'Norbert Bodkin' }, '5' => { 'EMail' => 'sue@thnwb.com', 'Name' => 'Sue Soup' } }, '2' => { '3' => { 'EMail' => 'max@thnwb.com', 'Name' => 'Max Grappler' } } );
    *note the parentheses at the outermost level, not curlies.

    I don't know if this helps, but it seems like you're super-close to getting it.

    ~CS

ACK! ACK! ACK!!
by George_Sherston (Vicar) on Nov 17, 2001 at 01:28 UTC
    You're both right, but not in any way that makes me look good...

    The problem IS with the way I'm defining my data structure... sort of... in that, although I define it ok, I then do nothing with it except print it with Data::Dumper. The hash I'm trying to iterate through is a DIFFERENT HASH! I just wrote the wrong ****** name... /me reaches for the Slapping Trout.

    I told you it would be embarrassing... thanks for helping me find it!

    George Sherston

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2014-04-19 18:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (483 votes), past polls