[英]hash of hashes in Perl, get keys
I have the following code: 我有以下代码:
$num = keys %{$hash_o_count{$genename}{$allname}};
print $num."\n";
$hash_o_count{$genename}{$allname} = $num + 1;
I'd like to have the number of keys I have in a nested hash, but I don't know how to get it even though an extensive research on Google. 我想拥有嵌套哈希中拥有的键的数量,但是即使对Google进行了广泛的研究,我也不知道如何获得它。
Any help? 有什么帮助吗? Thanks. 谢谢。
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
my %hash;
$hash{level1}{level2}{level3} =
{
one => 'apple',
two => 'orange'
};
my $bottom_level_keys = keys %{ $hash{level1}{level2}{level3} };
say $bottom_level_keys. " keys at the bottom level";
#!/usr/bin/perl
use strict;
use warnings;
my %HoH = (
flintstones => {
husband => "fred",
pal => "barney",
},
jetsons => {
husband => "george",
wife => "jane",
"his boy" => "elroy", # Key quotes needed.
},
simpsons => {
husband => "homer",
wife => "marge",
kid => "bart",
},
);
my $cnt=0;
for my $family ( keys %HoH ) {
$cnt++;
for my $role ( keys %{ $HoH{$family} } ) {
$cnt++;
}
}
print "$cnt"; #Output is 11
A bit modified version of code from Programming Perl . 来自Perl编程的代码的修改版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.