Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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' => '', 'Name' => 'Norbert Bodkin' }, '5' => { 'EMail' => '', 'Name' => 'Sue Soup' } }, '2' => { '3' => { 'EMail' => '', '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/ 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

Replies are listed 'Best First'.
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' => '', 'Name' => 'Norbert Bodkin' }, '5' => { 'EMail' => '', 'Name' => 'Sue Soup' } }, '2' => { '3' => { 'EMail' => '', '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' => '', 'Name' => 'Norbert Bodkin' }, '5' => { 'EMail' => '', 'Name' => 'Sue Soup' } }, '2' => { '3' => { 'EMail' => '', '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.


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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://125917]
Approved by root
[choroba]: (well, it comes from the 14th century, so labelling it as "modern" doesn't seem appropriate)
[Your Mother]: "They" is becoming accepted but it irritates me sometimes. I tend to just pick she or he randomly or try to use "one."
LanX thinks it is appropriate here :)
[Your Mother]: They would think so.
[jdporter]: ok, I need a recipe for piping lines "through" an external program which is itself a filter
[jdporter]: without using a tmp file
[1nickt]: tobyink perl -MTypes::Standard= is_Int -Mstrict -wE 'say 1 if is_Int 1.0'
[jdporter]: so that I can use the existing expand unix util. Otherwise, I'll probably use Text::Tabs.
[1nickt]: pryrt I guess I don;t really care if user 42 logs on as 42.0 ... more of an academic question at this point.
[LanX]: jdporter: open PIPE,'-|' ?

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (13)
As of 2017-05-24 20:24 GMT
Find Nodes?
    Voting Booth?