2014-06-09 4 views
1

나는 다음과 같은 코드가 있습니다해시 키를 얻을

$num = keys %{$hash_o_count{$genename}{$allname}}; 
print $num."\n"; 
$hash_o_count{$genename}{$allname} = $num + 1; 

은 내가 중첩 된 해시에있는 키의 수를 가지고 싶지만 내가 얻는 방법을 모르겠어요 비록 Google에 대한 광범위한 연구가 있긴하지만.

어떤 도움이 필요합니까? 감사합니다. .

+0

, 당신이 원하는 것을 분명하지 않다 http://perlmonks.org/?node=References+quick+reference – ysth

+1

를 참조하십시오. 당신이 작성한 것은'$ num'에 해시 키의 수를 넣었지만 방금 추출한 키 수를 데이터 구조의 해시 참조로 덮어 씁니다. 멀티 레벨 해시의 예를 보여주고 계산할 키를 지정하고 그 값으로 무엇을하고 싶은지 설명하십시오. – Borodin

답변

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"; 
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입니다. 당신은 단지 하나의 중첩 된 해시 키의 수를 원하는 경우

Demo

관련 문제