펄 :

2013-05-15 2 views
4

샘플 코드 고려 해시의 해시의 해시 역 참조 : 내가 참조로 반환펄 :

$VAR1 = { 
     'en' => { 
       'new' => { 
         'style' => 'defaultCaption', 
         'tts:fontStyle' => 'bold', 
         'id' => 'new' 
        }, 
       'defaultCaption' => { 
            'tts:textAlign' => 'left', 
            'tts:fontWeight' => 'normal', 
            'tts:color' => 'white', 

           } 
      }, 
     'es' => { 
       'defaultSpeaker' => { 
            'tts:textAlign' => 'left', 
            'tts:fontWeight' => 'normal', 

           }, 
       'new' => { 
         'style' => 'defaultCaption', 
         'tts:fontStyle' => 'bold', 
         'id' => 'new' 
        }, 
       'defaultCaption' => { 
            'tts:textAlign' => 'left', 
            'tts:fontWeight' => 'normal', 

           } 
      } 
    }; 

을 반환 \ %의 해시

어떻게이 역 참조합니까?

답변

7

%$hash. 자세한 내용은 http://perldoc.perl.org/perlreftut.html을 참조하십시오.

당신의 해시 함수 호출에 의해 반환되는 경우 중 하나를 수행 할 수 있습니다

my $hash_ref = function_call(); 
for my $key (keys %$hashref) { ... # etc: use %$hashref to dereference 

또는 :

my %hash = %{ function_call() }; # dereference immediately 

액세스 값에 해시 내에서, 당신은 -> 연산자를 사용할 수 있습니다.

$hash->{en}; # returns hashref { new => { ... }. defaultCaption => { ... } } 
$hash->{en}->{new};  # returns hashref { style => '...', ... } 
$hash->{en}{new};  # shorthand for above 
%{ $hash->{en}{new} }; # dereference 
$hash->{en}{new}{style}; # returns 'defaultCaption' as string 
+0

처럼 그것을 할 수 있습니까 그것을 완전히 역 참조하고 싶다. 당신이 말한 것을 시도했습니다. 작동하지 않습니다. 첫 번째 해시를 참조 해제합니다. – dreamer

+0

당신이하려는 일에 대한 코드 예제를 줄 수 있습니까? 제가 언급 한 튜토리얼은 매우 유용합니다, btw. – rjh

3

시도 뭔가 아래와 같이, 당신을 위해 도움이 될 수 있습니다

my %hash = %{ $VAR1}; 
     foreach my $level1 (keys %hash) { 
      my %hoh = %{$hash{$level1}}; 
      print"$level1\n"; 
      foreach my $level2 (keys %hoh) { 
       my %hohoh = %{$hoh{$level2}}; 
       print"$level2\n"; 
       foreach my $level3 (keys %hohoh) { 
         print"$level3, $hohoh{$level3}\n"; 
       } 
      } 
     } 

을 또한, 사용자가 특정 키에 액세스하려는 경우, 당신은

my $test = $VAR1->{es}->{new}->{id};

+0

Genius! 감사! – jouell