0

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:

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;
}

And following code works:

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;
}

Please someone can tell me, what is the problem please ?

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 );

In the first case, the key has not been found, and in the second the key has been found ...

EDIT

First case:

$ perl myTest.pl
Key not found

Second case:

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

EDIT

Another thing, i think it will be usefull

$ 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)
7
  • Usually it is best to boil down your problem to a minimal version. Mar 29, 2012 at 8:31
  • i do not know how to boil down my problem to a minimal version. But i can add a full example, i it can help.
    – Xavier S.
    Mar 29, 2012 at 8:37
  • 1
    What do you mean by does not work? Could you please provide some sample data for input and expected output? Mar 29, 2012 at 8:39
  • Does not work means the key has not been found. And if i dump the hashtable, the same code works, and it can find the right key...
    – Xavier S.
    Mar 29, 2012 at 9:04
  • @binogure if you've got a version of your code that works, why do you need help?
    – MattLBeck
    Mar 29, 2012 at 10:42

1 Answer 1

0

Ok, i found the answer at my own question

Extract from perlfunc(1) :

There is a single iterator for each hash, shared by all "each", "keys", and "values" function calls in the program; it can be reset by reading all the elements from the hash, or by evaluating "keys HASH" or "values HASH".

1
  • Congrats on the fix! When you are able, please make sure to mark your answer as 'accepted' so other will see your question has been answered and be able to learn from your solution. Cheers~ Mar 30, 2012 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.