我有以下代码:
$num = keys %{$hash_o_count{$genename}{$allname}};
print $num."\n";
$hash_o_count{$genename}{$allname} = $num + 1;
我想拥有嵌套哈希中的密钥数量,但即使对Google进行了广泛的研究,我也不知道如何获得密钥。
有任何帮助吗? 感谢。
答案 0 :(得分:1)
#!/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";
答案 1 :(得分:0)
#!/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
来自Programming Perl的代码的修改版本。