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

How to iterate through a hash of hashes?

by matt.schnarr (Sexton)
on Jan 11, 2005 at 19:18 UTC ( #421382=perlquestion: print w/ replies, xml ) Need Help??
matt.schnarr has asked for the wisdom of the Perl Monks concerning the following question:

Hi there,

I am attempting to iterate through a hash of hashes, however, I am not sure of the correct way to do so.

I have the following hash:

$hash{"192.168.0.1"}{"randy"} = "thomas"; $hash{"192.168.0.1"}{"ken"} = "samual"; $hash{"192.168.0.2"}{"jessie"} = "jessica"; $hash{"192.168.0.2"}{"terry"} = "ryan";

I would like to be able to iterate over the entire set and print out the 4 values: thomas, samual, jessica and ryan

Any help would be greatly appreciated!!

Thanks!

Matt

Comment on How to iterate through a hash of hashes?
Download Code
Re: How to iterate through a hash of hashes?
by Tanktalus (Canon) on Jan 11, 2005 at 19:24 UTC

    see keys...

    foreach my $outter (keys %hash) { foreach my $inner (keys %{$hash{$outter}}) { print "$outter - $inner = ", $hash{$outter}{$inner}, "\n"; } }
Re: How to iterate through a hash of hashes?
by friedo (Prior) on Jan 11, 2005 at 19:26 UTC
    For a two level hash, simply create two nested loops.

    foreach my $okey( keys %hash ) { my $inner = $hash{$okey}; foreach my $ikey( keys %$inner ) { print $inner->{$ikey}, "\n"; } }
Re: How to iterate through a hash of hashes?
by Zaxo (Archbishop) on Jan 11, 2005 at 19:28 UTC

    { local ($,, $\) = ($/, $/); print values %{$_} for values %hash; }
    The output seperators provide the format, the for loop puts hash references in $_.

    After Compline,
    Zaxo

Re: How to iterate through a hash of hashes?
by davido (Archbishop) on Jan 11, 2005 at 19:49 UTC

    This method recursively drops down to the bottom level of any depth of hash of hashes, and prints the end values.

    use strict; use warnings; my %hash = ( "192.168.0.1" => { "randy" => "thomas", "ken" => "samual" }, "192.168.0.2" => { "jessie" => "jessica", "terry" => "ryan" } ); traverse( \%hash ); sub traverse { if( ref( $_[0] ) =~ /HASH/ ) { traverse( $_[0]{$_} ) foreach keys %{$_[0]}; } else { print "$_[0]\n"; } }

    The limit to levels of depth is 100 for most Perls, unless you turn off the warning for deep recursion with no warnings qw/recursion/; inside the traverse()sub.


    Dave

Re: How to iterate through a hash of hashes?
by elwarren (Curate) on Jan 11, 2005 at 20:08 UTC
    Just to be different, save a step and use each:
    #!/usr/bin/perl use warnings; use strict; my %hash = (); $hash{"192.168.0.1"}{"randy"} = "thomas"; $hash{"192.168.0.1"}{"ken"} = "samual"; $hash{"192.168.0.2"}{"jessie"} = "jessica"; $hash{"192.168.0.2"}{"terry"} = "ryan"; foreach my $ip (keys %hash) { while (my ($key, $value) = each %{ $hash{$ip} } ) { print "$key = $value \n"; } }
Re: How to iterate through a hash of hashes?
by wee (Beadle) on Jan 11, 2005 at 20:38 UTC
    You can use map inside a foreach loop as well:
    my %hash; $hash{"192.168.0.1"}{"randy"} = "thomas"; $hash{"192.168.0.1"}{"ken"} = "samual"; $hash{"192.168.0.2"}{"jessie"} = "jessica"; $hash{"192.168.0.2"}{"terry"} = "ryan"; foreach my $key1 ( keys %hash ) { map { print $hash{$key1}->{$_}, "\n" } keys %{$hash{$key1}}; }
Re: How to iterate through a hash of hashes?
by xtype (Deacon) on Jan 11, 2005 at 21:53 UTC
    Zaxo++

    And then there is always this:
    use Data::Dumper; print Dumper(\%hash);
Re: How to iterate through a hash of hashes?
by bsb (Priest) on Jan 11, 2005 at 22:30 UTC
    use Data::Rmap; rmap { print "$_\n" } \%hash thomas samual ryan jessica
Re: How to iterate through a hash of hashes?
by DrHyde (Prior) on Jan 12, 2005 at 09:39 UTC
    I love the smell of homework in the morning.

    Show us what you've tried, and explain how you've tried debuggering it.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://421382]
Front-paged by friedo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2015-07-04 01:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls