2012-10-01 5 views
-2

해시 값에 액세스하는 데 문제가 있습니다. 무엇이 잘못되었는지 잘 모르겠습니다. Perl은하지만 해시는별로 없습니다.해시 해시의 값에 액세스하기

해시 해시에서 해시 값에 액세스하려고합니다. 내가 아니라 직접 해시 키를 통해 itereting하고 때 목록에 액세스 할 수있는 해시에게

sub buildList 
{ 
    my ($name,$gender,$father,$mother,$age); 
    my %bear_ref=(); 

    open IN, "<input.txt" or die "can't open file"; 

    while(<IN>) { 
     ($name, $gender, $father, $mother, $age) = split(/:/); 
     $bear_ref{ $name } { 'gender' } = $gender; 
     $bear_ref{ $name } { 'mother' } = $father; 
     $bear_ref{ $name } { 'father' } = $mother; 
     $bear_ref{ $name } { 'age' } = $age; 
    } 
    close IN; 
    return \%bear_ref; 
} 

을 구축 할 경우 다음

이다. 그래서 나는 어떤 도움을 주시면 감사하겠습니다

for my $name (keys %$ref) { 
    $father= $ref->{ $name }->{ 'father'}; # works 
    $mother= $ref->{ $name }->{ 'mother'}; # works 
    getTree($name, $ref); 
} 

sub getTree 
{ 
    my $bear = shift; 
    my $ref = shift; 
    my ($father, $mother); 
    $father= $ref->{ $name }->{ 'father'}; # doesn't work...have also tried with %$ref-> 
    $mother= $ref->{ $name }->{ 'mother'}; # doesn't work...have also tried with %$ref-> 
    print "$father : $mother\n"; 

} 

내가로부터 값을 받고 있어요 방식 "키() 루프"함께 할 수있는 뭔가가 있으리라 믿고있어.

+2

항상 ['use strict;'] (http://perldoc.perl.org/strict.html) 및 ['경고 사용;] (http://perldoc.perl.org/warnings.html)까지 당신은 그것이 왜 추천되는지 정확하게 알 수 있습니다. –

답변

3

$namegetTree 서브의 $bear이어야합니다.

sub getTree 
{ 
    my $bear = shift; 
    my $ref = shift; 
    my ($father, $mother); 
    ## note that I am using $bear instead of $name since $name is not defined 
    $father= $ref->{ $bear }->{ 'father'}; 
    $mother= $ref->{ $bear }->{ 'mother'}; 
    print "$father : $mother\n"; 
} 

참고 :use strictuse warnings 도움이 될 것입니다.

+0

와우, 오케이 지금은 바보 같아. 감사. 그래, 나는 엄격한 경고를 사용하지만 출력을 잊어 버렸다. 어느 쪽이든 지금 당장 가지고 있다고 생각합니다. 다시 한번 감사드립니다. – user1712832

관련 문제