2012-03-29 2 views
0

두 개의 해시 테이블과 일치해야하지만 해당 키가 일치하지 않습니다. 그래서 나는 각 해시 테이블에 도달하고 값으로 매치한다. 그래서 코드를하지 않는 다음 작품 :펄에서 해시 테이블에 도달

sub _findHashElt 
{ 
    my ($this, $hashTarget) = @_; 

    my $isFound = 0; 
    my $isItemFound = 1; 
    my $isHashFound = 0; 
    my $result = undef; 
    my %hashTable=%{$this->{templates}}; 
    my %hashTargetHash=%{$hashTarget}; 
    my %hashSubTable = undef; 

    while ( (my ($key, $value) = each(%hashTable)) 
      and ($isFound   == 0    )) 
    { 
     $isItemFound = 1; 
     $isHashFound = 0; 

     #################################### 
     # DOES NOT WORK 
     %hashSubTable = %{$hashTable{$key}}; 
     #################################### 

     while ( (my ($subKey, $subValue) = each(%hashTargetHash)) 
       and ($isItemFound   == 1     )) 
     { 
      $isItemFound = 0; 
      while ( (my ($subTableKey, $subtableValue) = each(%hashSubTable)) 
        and ($isItemFound      == 0     )) 
      { 
       $isHashFound = 1; 
       $isItemFound = ($subValue eq $subtableValue) ? 1 : 0; 
      } 
     } 

     if ($isItemFound == 1 && $isHashFound == 1) { 
      $isFound = 1; 
      $result = $key; 
     } 
    } 

    return $result; 
} 

그리고 다음 코드는 작동 :

sub _findHashElt 
{ 
    my ($this, $hashTarget) = @_; 

    my $isFound = 0; 
    my $isItemFound = 1; 
    my $isHashFound = 0; 
    my $result = undef; 

    my %hashTable=%{$this->{templates}}; 
    my %hashTargetHash=%{$hashTarget}; 
    my %hashSubTable = undef; 

    while ( (my ($key, $value) = each(%hashTable)) 
      and ($isFound   == 0    )) 
    { 
     $isItemFound = 1; 
     $isHashFound = 0; 

     while ( (my ($subKey, $subValue) = each(%hashTargetHash)) 
       and ($isItemFound   == 1     )) 
     { 
      #################################### 
      # WORK 
      %hashSubTable = %{$hashTable{$key}}; 
      #################################### 

      $isItemFound = 0; 

      while ( (my ($subTableKey, $subtableValue) = each(%hashSubTable)) 
        and ($isItemFound      == 0     )) 
      { 
       $isHashFound = 1; 

       $isItemFound = ($subValue eq $subtableValue) ? 1 : 0; 
      } 
     } 

     if ($isItemFound == 1 && $isHashFound == 1) { 
      $isFound = 1; 
      $result = $key; 
     } 
    } 

    return $result; 
} 

사람은 문제가 무엇인지하시기 바랍니다이며, 말해 줄 수주십시오?

EDIT

첫째 ...

편집 제 경우

############################################# 
################# TEST PART ################# 
############################################# 
my $this = {"templates" => {}}; 
my $example = {'key0' => {'key00' => 'test00', 'key01' => 'test01', 'key02' => '0', 'key03' => 'test03'}, 
       'key1' => {'key00' => 'test10', 'key01' => 'test11', 'key02' => '1', 'key03' => 'test13'}, 
       'key2' => {'key00' => 'test20', 'key01' => 'test21', 'key02' => '1', 'key03' => 'test23'}, 
       'key3' => {'key00' => 'test30', 'key01' => 'test31', 'key02' => '0', 'key03' => 'test33'}}; 
my $expected = {'key00' => 'test00', 'key01' => 'test01', 'key02' => '0', 'key03' => 'test03'}; 
$this->{templates}=$example; 
_findHashElt($this, \%expected); 

키가 발견되지 않았으며, 두 번째의 키가 밝혀졌다 사례 :

$ perl myTest.pl 
Key not found 

두 번째 경우 :

$ perl myTest.pl 
Key found 
{ 
    key01 : test01 
    key00 : test00 
    key03 : test03 
    key02 : 0 
} 

편집

또 한가지, 내가 그것을

$ perl --version 

This is perl, v5.10.1 (*) built for i686-cygwin-thread-multi-64int 
(with 13 registered patches, see perl -V for more detail) 
+0

일반적으로 문제를 최소 버전으로 낮추는 것이 가장 좋습니다. –

+0

나는 최소한의 버전으로 내 문제를 종결하는 법을 모르겠다. 하지만 전체 예제를 추가 할 수 있습니다. 도움이 될 수 있습니다. –

+1

'작동하지 않는다 '는 것은 무엇을 의미합니까? 입력 및 예상 출력에 대한 샘플 데이터를 제공해 주시겠습니까? –

답변

0

확인 유용 할 것이라고 생각, 난 내 자신의 질문에 대답을 발견

perlfunc (1)에서 추출한 내용 :

각 해시마다 하나의 반복자가 있으며 프로그램의 모든 "each", "keys"및 "values"함수 호출에 의해 공유됩니다. 해시에서 모든 요소를 ​​읽거나 " 해시 키"또는 "해시 값"을 평가하여 을 재설정 할 수 있습니다.

+0

수정을 축하합니다! 가능한 경우 답변을 '수락'으로 표시하여 다른 사람이 귀하의 질문에 답변하고 귀하의 솔루션에서 배울 수 있도록하십시오. 건배 ~ –

관련 문제