user855
user855

Reputation: 19918

How can I print a hash of hashes of hashes in Perl?

I have a $map{k1}{k2}{k3}{k4}.

How can I write the loop correctly to print all values? The following does not work.

for my $k1 (sort keys %tripletsCountMap) {
    for my $k2 (sort keys %$tripletsCountMap{k1}){
        for my $k3 (sort keys %$tripletsCountMap{k1}{k2}) {
            for my $k4 (sort keys %$tripletsCountMap{k1}{k3}{k3}){
                print "$k1 $k2 $k3 $k4\n";
            }
        }
    }
}

Upvotes: 2

Views: 10544

Answers (8)

gorilla
gorilla

Reputation: 672

If this is for debugging or similar purposes, it's probably better to use Data::Dumper to do this sort of thing. It's intelligent enough to follow through the data structure and get it right.

Upvotes: 4

cmd
cmd

Reputation: 1

yet another way:

 while (my ($first,$second) = each (%hash_hash_hash_hash)) {
    while (my ($second, $third) = each (%$second)) {
        while (my ($third, $fourth) = each (%$third)) {
            while (my ($fourth, $value) = each (%$fourth)) {
                print "$first\t$second\t$third\t$fourth\t$value\n";
            }
        }
    }
}

Upvotes: 0

Dixit Wadhwani
Dixit Wadhwani

Reputation: 227

you can use following code: this will work

while(my($key1,$value1)=each(%hash_hash)){
  while(my($key2,$value2)=each(%$value1)){
       print $key1."=".$key2."=".$value2."\n";
  }

}

Upvotes: 0

Hynek -Pichi- Vychodil
Hynek -Pichi- Vychodil

Reputation: 26121

Poor man's Dumper:

sub trace {
    my ( $val, $path ) = @_;
    my $ref = ref $val;
    if ( $ref eq '' ) {
        print "$path = $val\n";
    }
    elsif ( $ref eq 'HASH' ) {
        trace( $val->{$_}, $path . "{$_}" ) for keys %$val;
    }
    elsif ( $ref eq 'ARRAY' ) {
        trace( $val->[$_], $path . "[$_]" ) for 0 .. $#$val;
    }
    else {
        warn "I don't know what to do with $ref at $path\n";
    }
}

trace($map, '$map->');

Upvotes: 2

kriss
kriss

Reputation: 24177

$k1 is the variable, k1 is a bareword.

perl -e '%h = (1 => 2, "k1" => 3); $k1 = 1; printf "%d %d\n", $h{$k1}, $h{k1}'

2 3

Then if you use hash reference be cautious to use scalar variables to store them.

perl -e '$h = {1 => 2, "k1" => 3}; $k1 = 1; printf "%d %d\n", $h->{$k1}, $h->{k1}'

2 3

If you happened to write something like nothing will work as expected:

perl -e '%h = {1 => 2, "k1" => 3}; $k1 = 1; printf "%d %d\n", $h->{$k1}, $h->{k1}'

0 0

If the bareword is not the problem (it probably is), then you should carefully check how you built your map.

Upvotes: 1

Sinan Ünür
Sinan Ünür

Reputation: 118128

Note that there is a difference between k1 and $k1.

for my $k1 (sort keys %tripletsCountMap) {
    for my $k2 (sort keys %{ $tripletsCountMap{$k1} }){
        for my $k3 (sort keys %{ $tripletsCountMap{$k1}{$k2} }) {
            for my $k4 (sort keys %{ $tripletsCountMap{$k1}{$k2}{$k3} }){
                printf "$k1 $k2 $k3 $k4: $tripletsCountMap{$k1}{$k2}{$k3}{$k4}\n";
            }
        }
    }
}

Better yet:

use Data::Dumper;
print Dumper \%tripletsCountMap;

And, why are you sorting the keys? I understand the point @ysth in the comments below. I am just not in the habit of sorting the keys of a hash when I iterate over them unless there is some explicit output related requirement.

Upvotes: 13

ysth
ysth

Reputation: 98398

When using % to dereference an expression, the expression must be enclosed in {} unless it's a simple scalar (e.g. %$href).

I recommend you read http://perlmonks.org/?node=References+quick+reference.

Upvotes: 4

PP.
PP.

Reputation: 10864

You're printing all the keys but not the final value.

In the most inner loop add:


my $val = $map{$k1}{$k2}{$k3}{$k4};
print "$val\n"; 

Upvotes: 0

Related Questions