How to compare 2 hashes of arrays by values(not keys)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aurekha
    New Member
    • Sep 2007
    • 34

    How to compare 2 hashes of arrays by values(not keys)

    Hi
    I have hashes with arrays to its keys like,
    %h1 = ('a'=>['abc','def'],
    'b'=>['ghi','jkl'],
    'c'=>['hop','uio']
    );

    %h2 = ('a'=>['abc','def'],
    'b'=>['wert','wer']);
    then, how can i compare the 2 hashes(based on values. not keys) and get distinct values ?


    Any solution will be appreciated.... .
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    The question is not very clear. I am not understanding with what criteria you want to compare the values of hashes(which are inturn arrays-compare lengths or elements of array??) . Also, let us know what have you tried so far.
    However, to begin with if you are trying to get values of the hash, you may use 'values' keyword.
    e.g.
    Code:
     foreach(values %h1) {
      print "@{$_}\n";   ## will print values of hash %h1(the array elements)
    }

    Comment

    • aurekha
      New Member
      • Sep 2007
      • 34

      #3
      Thanks for responding.
      i want to compare the values of %h1 with values of %h2 and the final result will be

      ( the values of %h1, which are in %h1 and not in %h2)
      Result is :

      ['ghi' , 'jkl' ]
      [ 'hop' ' uio']

      (since, these are not in %h2).
      I want the above output. How can i do this?????

      Comment

      • nithinpes
        Recognized Expert Contributor
        • Dec 2007
        • 410

        #4
        Originally posted by aurekha
        Thanks for responding.
        i want to compare the values of %h1 with values of %h2 and the final result will be

        ( the values of %h1, which are in %h1 and not in %h2)
        Result is :

        ['ghi' , 'jkl' ]
        [ 'hop' ' uio']

        (since, these are not in %h2).
        I want the above output. How can i do this?????
        You can make use of Data::Compare module to compare data structures in perl.

        [CODE=perl]
        use Data::Compare;
        use strict;

        my %h1 = ('a'=>['abc','def'],
        'b'=>['ghi','jkl'],
        'c'=>['hop','uio']
        );
        my %h2 = ('a'=>['abc','def'],
        'b'=>['wert','wer']);

        foreach my $x (values %h1) {
        my $i=0;
        foreach my $y (values %h2) {
        $i=1 if(Compare($x,$ y));
        }
        print "@{$x}\n" if($i==0);
        }
        [/CODE]

        Comment

        • aurekha
          New Member
          • Sep 2007
          • 34

          #5
          Thanks alot. This seems,it solves 99% of my problem.But
          in my hashes are like,
          my %h1 = ('a'=>['abc','def'],
          'b'=>['ghi','jkl'],
          'c'=>['hop','uio'],
          'd'=>['iooo','qwe']
          );
          my %h2 = ('x'=>['abc','def'],
          'b'=>['wert','wer']
          'd'=>['iooo','qwe']
          );
          Even though, the values are same, I want the output as,
          I want to match both keys and values.
          So the Optput will come as,
          ['iooo','qwe'].

          so, for your code, i add,


          use Data::Compare;
          use strict;
          my %h1 = ('a'=>['abc','def'],
          'b'=>['ghi','jkl'],
          'c'=>['hop','uio'],
          'd'=>['iooo','qwe']
          );
          my %h2 = ('x'=>['abc','def'],
          'b'=>['wert','wer'],
          'd'=>['iooo','qwe']
          );
          foreach my $x (values %h1)
          {
          my $i=0;
          foreach my $y (values %h2)
          {
          $i=1 if(Compare($x,$ y) && Compare($h1{$x} ,$h2{$y}) );
          }
          print "@{$x}\n" if($i==0);
          }



          but, This doesn't work.... Please help me out..... Thank you

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            first you said:

            how can i compare the 2 hashes(based on values. not keys)

            now you say:

            I want to match both keys and values

            which is it?

            Comment

            • aurekha
              New Member
              • Sep 2007
              • 34

              #7
              sorry for inconvenience.

              As per my Hod Instructions and also regarding my application, ineed to do the aleternate post also..


              Firstly I tried, but didn't get result.
              How can I Solve this problem. Please help me out.

              Comment

              • nithinpes
                Recognized Expert Contributor
                • Dec 2007
                • 410

                #8
                Originally posted by aurekha
                Thanks alot. This seems,it solves 99% of my problem.But
                in my hashes are like,
                my %h1 = ('a'=>['abc','def'],
                'b'=>['ghi','jkl'],
                'c'=>['hop','uio'],
                'd'=>['iooo','qwe']
                );
                my %h2 = ('x'=>['abc','def'],
                'b'=>['wert','wer']
                'd'=>['iooo','qwe']
                );
                Even though, the values are same, I want the output as,
                I want to match both keys and values.
                So the Optput will come as,
                ['iooo','qwe'].
                Your initial question is entirely different from your current requirement. However, you can make use of the same code and modify it to suit your requirement.
                To display the value of the key-value pairs that are common in both hashes:
                Code:
                use Data::Compare;
                use strict;
                 
                my %h1 = ('a'=>['abc','def'],
                'b'=>['ghi','jkl'],
                'c'=>['hop','uio'],
                'd'=>['iooo','qwe']
                );
                my %h2 = ('x'=>['abc','def'],
                'b'=>['wert','wer'],
                'd'=>['iooo','qwe']
                );
                
                foreach my $x (keys %h1) {
                  my $i=0;
                 foreach my $y (keys %h2) {
                  $i=1 if(Compare($x,$y) && Compare($h1{$x},$h2{$y}) );
                }
                  print "@{$h1{$x}}\n" if($i==1);
                }
                Output will be:
                Code:
                 iooo qwe

                Comment

                • aurekha
                  New Member
                  • Sep 2007
                  • 34

                  #9
                  Thanks alot nithinpes

                  Your Logic wroks well for me.


                  Thank alot.


                  Its great forum.

                  Comment

                  • aurekha
                    New Member
                    • Sep 2007
                    • 34

                    #10
                    I tried like this........... ..........

                    while(my($k1,$v 1) = each(%h1))
                    {
                    my $i = 0;
                    while(my($k2,$v 2) = each(%h2))
                    {
                    $i = 1 if(Compare($v1, $v2) && Compare($k1,$k2 ));
                    }
                    print "@{$v1}\n" if($i==0);
                    }


                    This works fine and yours also works for me............. ..
                    Thank you............

                    Comment

                    • sally123
                      New Member
                      • Mar 2012
                      • 3

                      #11
                      Comparing hash of arrays

                      Hello,
                      I have a different question on hash of arrays.
                      The programming language I am using is perl.

                      I have the following:
                      %a=(A1=>[UP1,UP3,UP6],
                      A2=>[UP1,UP6],
                      A3=>[UP2,UP10],
                      );

                      %b=(UP1=>[UP2,UP3,UP7],
                      UP2=>[UP10,UP9,UP3],
                      UP5=>[UP1,UP11],
                      );

                      Please keep in mind that the keys are different, so I can't compare them.

                      Desired output:
                      A1 has values UP1, UP3, UP6.
                      Among these, only UP1 is a key in %b.
                      The values of UP1 are UP2, UP3, UP7. I have to compare these 3 values with the values in %a. Whichever key in %a has any of these values, I have to print them.

                      Example: A1 has UP1, UP3, UP6. UP1 key has values UP2, UP3, UP7. Among these, UP2 has the key A3 and UP3 has the key A1.
                      So output is: A1-A3,A1.

                      I hope this explanation is clear. Please let me know if there are any doubts.

                      Comment

                      Working...