开发者

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 开发者_如何学JAVAmy $k4 (sort keys %$tripletsCountMap{k1}{k3}{k3}){
                print "$k1 $k2 $k3 $k4\n";
            }
        }
    }
}


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.


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.


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.


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->');


$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.


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"; 


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";
  }

}


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";
            }
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜