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

%h1 = ( "key1","value1","Key2", "value2" ); %h2 = ( "Key1","value1","key2", "value2" ); # Declared two hashes %hoh = ( "key1" => \%h1, "key2" => \%h2 ); foreach $key (keys %hoh){ $innerHashRef = $hoh{$key}; foreach $innerHashKey (keys %$innerHashRef){ print "Inner Key: $innerHashKey"; <-- this does not work } }
Can someone help me please! Thanks!!!
Edited by boo_radley : code tags.

Replies are listed 'Best First'.
Re: Please Help wiht Hashes
by particle (Vicar) on May 10, 2002 at 20:44 UTC
    #!/usr/bin/perl -w use strict; my %h1 = ( "key1","value1","Key2", "value2" ); my %h2 = ( "Key1","value1","key2", "value2" ); my %hoh = ( "key1" => \%h1, "key2" => \%h2 ); foreach my $key (keys %hoh){ my $innerHashRef = $hoh{$key}; foreach my $innerHashKey (keys %$innerHashRef){ print "Inner Key: $innerHashKey\n"; } } #prints: Inner Key: Key2 Inner Key: key1 Inner Key: Key1 Inner Key: key2
    what doesn't work for you? maybe strict and warnings will give you some hints?

    ~Particle *accelerates*

Re: Please Help wiht Hashes
by chicks (Scribe) on May 10, 2002 at 20:43 UTC
    What doesn't work about it? It seems fine. I tweaked it a bit to demonstrate:
    %h1 = ( "key1","value1","Key2", "value2" ); %h2 = ( "Key3","value3","key4", "value4" ); # Declared two hashes %hoh = ( "key1" => \%h1, "key2" => \%h2 ); foreach $key (keys %hoh) { $innerHashRef = $hoh{$key}; foreach $innerHashKey (keys %$innerHashRef) { print "Inner Key: $innerHashKey\n"; # this does work } }
Re: Please Help with Hashes
by cLive ;-) (Prior) on May 11, 2002 at 11:58 UTC
    You aren't printing a \n after the print statement. Some (many?) Perl versions need at least one for output to display (or so I've found).

    .02

    cLive ;-)

    --
    seek(JOB,$$LA,0);

Re: Please Help wiht Hashes
by thelenm (Vicar) on May 10, 2002 at 20:43 UTC
    It works for me... where is it failing for you? What is it printing?

    Side note: you may want to wrap your code in <CODE> tags, so the Monks can easily download and test it.