How to iterate through Hash (of Hashes) in Perl?

后端 未结 9 891
無奈伤痛
無奈伤痛 2020-12-08 03:20

I have Hash where values of keys are other Hashes.

Example: {\'key\' => {\'key2\' => {\'key3\' => \'value\'}}}

How can I iterate throug

相关标签:
9条回答
  • 2020-12-08 04:02

    Also, please read through perldoc perldsc. You can learn about hashes in depth

    0 讨论(0)
  • 2020-12-08 04:03

    This post may be useful.

    foreach my $key (keys %hash) {
        foreach my $key2 (keys %{ $hash{$key} }) {
            foreach my $key3 (keys %{ $hash{$key}{$key2} }) {
                $value = $hash{$key}{$key2}->{$key3};
                # .
                # .
                # Do something with $value
                # .
                # .
                # .
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-08 04:03

    You will have to loop through it twice. i.e.

    while ( ($family, $roles) = each %HoH ) {
       print "$family: ";
       while ( ($role, $person) = each %$roles ) {
          print "$role=$person ";
       }
    print "\n";
    }
    
    0 讨论(0)
提交回复
热议问题