in reply to Need help looping through HoH

I think you're seeking keys, and its friends values and each. Together this rag-tag bunch of former special ops members travels the country in a custom van helping those in need.

Wait, no. They let you iterate over hash contents. I think I got them confused with the A Team. Must be the similar mohawks . . .

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^2: Need help looping through hash
by ewhitt (Scribe) on Aug 30, 2008 at 06:36 UTC
    I am trying this, but with no success. How far off base am I?
    for my $key (keys %$hash) { print "$hash->{'prefix'}->{$key}" }
      No cigar either...
      foreach my $k1 ( keys %{ $hash{'prefix'} } ) { for my $k2 ( keys %{ $hash{$k1} } ) { print("$k2\n"); } }
        As someone said, you can't just make stuff up and expect the computer to know what you want. :-)

        Slow down. Have another look at the links you've been given and use plenty of Data::Dumper at every step. Good luck!

        #!/usr/local/bin/perl use strict; use warnings; use Data::Dumper; $Data::Dumper::Indent = 1; $Data::Dumper::Sortkeys = 1; my %hash = ( 'summary' => { 'allotments' => 679 }, 'prefix' => { '32 ' => 425, '45 ' => 5, '41 ' => 2, '46 ' => 5, '44 ' => 1, '47 ' => 9, '43 ' => 1, '27 ' => 1, '48 ' => 212, '22 ' => 14, '29 ' => 2, '40 ' => 2 }, ); for my $key (keys %hash){ print qq{key: $key\n}; for my $subkey (keys %{$hash{$key}}){ print qq{\tsubkey: $subkey\n}; } }
        outputs
        key: summary subkey: allotments key: prefix subkey: 32 subkey: 45 subkey: 41 subkey: 44 subkey: 46 subkey: 47 subkey: 27 subkey: 43 subkey: 48 subkey: 29 subkey: 22 subkey: 40