Note that there are some explanatory texts on larger screens.

plurals
  1. POReach a hashtable in perl
    primarykey
    data
    text
    <p>I have to match two hashtable, but its keys does not match. So i reach each hashtable and match by values. So following code does not works:</p> <pre><code>sub _findHashElt { my ($this, $hashTarget) = @_; my $isFound = 0; my $isItemFound = 1; my $isHashFound = 0; my $result = undef; my %hashTable=%{$this-&gt;{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 &amp;&amp; $isHashFound == 1) { $isFound = 1; $result = $key; } } return $result; } </code></pre> <p>And following code works:</p> <pre><code>sub _findHashElt { my ($this, $hashTarget) = @_; my $isFound = 0; my $isItemFound = 1; my $isHashFound = 0; my $result = undef; my %hashTable=%{$this-&gt;{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 &amp;&amp; $isHashFound == 1) { $isFound = 1; $result = $key; } } return $result; } </code></pre> <p>Please someone can tell me, what is the problem please ?</p> <p><strong>EDIT</strong></p> <pre><code>############################################# ################# TEST PART ################# ############################################# my $this = {"templates" =&gt; {}}; my $example = {'key0' =&gt; {'key00' =&gt; 'test00', 'key01' =&gt; 'test01', 'key02' =&gt; '0', 'key03' =&gt; 'test03'}, 'key1' =&gt; {'key00' =&gt; 'test10', 'key01' =&gt; 'test11', 'key02' =&gt; '1', 'key03' =&gt; 'test13'}, 'key2' =&gt; {'key00' =&gt; 'test20', 'key01' =&gt; 'test21', 'key02' =&gt; '1', 'key03' =&gt; 'test23'}, 'key3' =&gt; {'key00' =&gt; 'test30', 'key01' =&gt; 'test31', 'key02' =&gt; '0', 'key03' =&gt; 'test33'}}; my $expected = {'key00' =&gt; 'test00', 'key01' =&gt; 'test01', 'key02' =&gt; '0', 'key03' =&gt; 'test03'}; $this-&gt;{templates}=$example; _findHashElt($this, \%expected ); </code></pre> <p>In the first case, the key has not been found, and in the second the key has been found ...</p> <p><strong>EDIT</strong></p> <p>First case:</p> <pre><code>$ perl myTest.pl Key not found </code></pre> <p>Second case:</p> <pre><code>$ perl myTest.pl Key found { key01 : test01 key00 : test00 key03 : test03 key02 : 0 } </code></pre> <p><strong>EDIT</strong></p> <p>Another thing, i think it will be usefull</p> <pre><code>$ 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) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload