By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,775 Members | 1,740 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,775 IT Pros & Developers. It's quick & easy.

Compare two complex Hash

P: 41
Hi all,

i have two hashes. i need 2 compare these hases & Results are stored in %different hash.

Please Help somebody.

Thanks
DK.



Expand|Select|Wrap|Line Numbers
  1. %hash1 = (    file1 => [            
  2.                             {
  3.                               line => '107',
  4.                               filename => 'SRC',
  5.                               source => '',
  6.                               target => [{ 
  7.                                                     C1 => '023',
  8.                                                     C2 => 'X',
  9.                                                 },],
  10.                             },
  11.                             {
  12.                               line => '21',
  13.                               filename => 'SRC',
  14.                               source => '',
  15.                               target => [{
  16.                                                     C1 => '139',
  17.                                                     C2 => '221FIFTH',
  18.                                              },],
  19.                             },
  20.                             {
  21.                               line => '1',
  22.                               filename => 'SRC',
  23.                               source => '',
  24.                               target => [{
  25.                                                     C1 => '004',
  26.                                                     C2 => '101STR',
  27.                                              },],
  28.                             },
  29.                     ]
  30.  
  31.            );
  32.  
  33. %hash2 = (    file2 => [        {
  34.                               line => '107',
  35.                               filename => 'TRG',
  36.                               target => '',
  37.                               source => [ 
  38.                                                 { 
  39.                                                     C1 => '023',
  40.                                                     C2 => 'X',
  41.                                                 },
  42.                                          ],
  43.                             },
  44.                             {
  45.                               line => '1',
  46.                               filename => 'TRG',
  47.                               target => '',
  48.                               source => [ 
  49.                                                 {
  50.                                                     C1 => '003',
  51.                                                     C2 => '101STHR',
  52.                                                 },
  53.                                              ],
  54.                             },
  55.                             {
  56.                               line_number => '21',
  57.                               filename => 'TRG',
  58.                               target => '',
  59.                               source => [ 
  60.                                                 {
  61.                                                     C1 => '1349',
  62.                                                     C2 => '221FIFTH',
  63.                                                 },
  64.                                              ],
  65.                             },
  66.                     ],
  67.                     );
  68.  

Above are 2 hashes

After comparing these two hashes, i need the results in below format


Expand|Select|Wrap|Line Numbers
  1. %different = (    diff => [            
  2.                             {
  3.                               line => '1',
  4.                               filename => 'SRC',
  5.                               source => '',
  6.                               target => [{
  7.                                                     C1 => '004',
  8.                                                     C2 => '101STR',
  9.                                              },],
  10.                             },
  11.                             {
  12.                               line => '1',
  13.                               filename => 'TRG',
  14.                               source => [ 
  15.                                                 {
  16.                                                     C1 => '003',
  17.                                                     C2 => '101STHR',
  18.                                                 },
  19.                                         ],
  20.                               target => '',
  21.                             },
  22.                             {
  23.                               line => '21',
  24.                               filename => 'SRC',
  25.                               source => '',
  26.                               target => [{
  27.                                                     C1 => '139',
  28.                                                     C2 => '221FIFTH',
  29.                                              },],
  30.                             },
  31.                             {
  32.                               line => '21',
  33.                               filename => 'TRG',
  34.                               source => [{
  35.                                                     C1 => '139',
  36.                                                     C2 => '221FIFTH',
  37.                                              },],
  38.                               target => '',
  39.                             },
  40.                         ]
  41.  
  42.            );
Aug 20 '08 #1
Share this Question
Share on Google+
13 Replies


KevinADC
Expert 2.5K+
P: 4,059
What have you tried?
Aug 20 '08 #2

chaosprime
P: 5
You probably need the CPAN module Data::Compare.
Aug 20 '08 #3

nithinpes
Expert 100+
P: 410
I believe you had posted a similar question sometime back and got a solution for it.
Aug 21 '08 #4

P: 6
Hi,

Were you able to figure out how to compare these two hashes?
I am trying to do the same and not sure how. I'm new to Perl as well.

Any help will be much appreciated.

Thanks!
Jan 30 '09 #5

numberwhun
Expert Mod 2.5K+
P: 3,503
@pranksk
You may want to examine the link referenced in the post prior to yours by Nithinpes. It is the OPs other thread and it may help you.

Regards,

Jeff
Jan 30 '09 #6

P: 6
Howevrr, that link points to a thread that explains how to merge the two hashes.

I just want to compare the 2 hashes (compare all keys and values of all parent and child nodes) and print out the differences in keys, values, etc
Jan 30 '09 #7

KevinADC
Expert 2.5K+
P: 4,059
Are the hashes simple hashes or complex hashes? A simple hash would be one level of key/value pairs, a complex hash would be anything different from that, like a hash of arrays or hash of hashes, etc. Also, what have you tried so far?
Jan 30 '09 #8

P: 6
Expand|Select|Wrap|Line Numbers
  1. $VAR1 = {
  2.           'files' => {
  3.                      'requirement_set' => {
  4.                                           'exclude_arch' => [
  5.                                                             'ARCH_C',
  6.                                                             'ARCH_A',
  7.                                                             'ARCH_E'
  8.                                                           ]
  9.                                         },
  10.                      'name' => [
  11.                                'file_1.m',
  12.                                'file_2.m'
  13.                              ]
  14.                    },
  15.           'version' => '1.0',
  16.           'macrodef' => {
  17.                         'local2' => {
  18.                                     'exclude_arch' => 'ARCH_F'
  19.                                   },
  20.                         'local3' => {
  21.                                     'exclude_arch' => 'ARCH_F'
  22.                                   },
  23.                         'local1' => {
  24.                                     'exclude_arch' => 'ARCH_E'
  25.                                   }
  26.                       },
  27.           'dir' => {
  28.                    'requirement_set' => {
  29.                                         'attribute' => {
  30.                                                        'content' => 'ATTR_B',
  31.                                                        'type' => 'device'
  32.                                                      },
  33.                                         'requirement_set' => {
  34.                                                              'exclude_arch' => [
  35.                                                                                'ARCH_E',
  36.                                                                                'ARCH_F'
  37.                                                                              ]
  38.                                                            }
  39.                                       }
  40.                  }
  41.         };
  42.  
  43.  
My second hash should be the same as this else I want to assert.
Jan 30 '09 #9

P: 6
Expand|Select|Wrap|Line Numbers
  1. sub _compare_hashes :PRIVATE {
  2.  
  3.     my ($data1, $data2) = @_;
  4.     my $found = 0;
  5.     my @missing;
  6.  
  7.     foreach my $parent_node1 (keys %data1) {
  8.         my @childNodes1 = $parent_node1->childNodes();
  9.         foreach $parent_node2 (keys %data2) {
  10.             my @childNodes2 = $parent_node2->childNodes();
  11.             if ($data1{$parent_node1} eq $data2{$parent_node2}) {
  12.                 print "Matched $data1{$parent_node1} & $data2{$parent_node2}\n";
  13.             } else {
  14.                 print "Mismatched $data1{$key1}\n";
  15.                 push @missing, $data1{$key1};
  16.             }
  17.             if ( (scalar(@childNodes1) > 1) || (scalar(@childNodes2) > 1) ) {
  18.                 foreach my $childNode1 (@childNodes1) { 
  19.                     foreach my $childNode2 (@childNodes2) { 
  20.                         _compare_hashes($childnode1, $childnode2)
  21.                     }
  22.                 }
  23.             }
  24.         }
  25.     }
  26. }
  27.  
Jan 30 '09 #10

P: 6
how do I use code tags so my posts display in a good format?
Jan 30 '09 #11

P: 6
Expand|Select|Wrap|Line Numbers
  1. $VAR1 = {
  2.           'files' => {
  3.                      'requirement_set' => {
  4.                                           'exclude_arch' => [
  5.                                                             'ARCH_C',
  6.                                                             'ARCH_A',
  7.                                                             'ARCH_E'
  8.                                                           ]
  9.                                         },
  10.                      'name' => [
  11.                                'file_1.m',
  12.                                'file_2.m'
  13.                              ]
  14.                    },
  15.           'version' => '1.0',
  16.           'macrodef' => {
  17.                         'local2' => {
  18.                                     'exclude_arch' => 'ARCH_F'
  19.                                   },
  20.                         'local3' => {
  21.                                     'exclude_arch' => 'ARCH_F'
  22.                                   },
  23.                         'local1' => {
  24.                                     'exclude_arch' => 'ARCH_E'
  25.                                   }
  26.                       },
  27.           'dir' => {
  28.                    'requirement_set' => {
  29.                                         'attribute' => {
  30.                                                        'content' => 'ATTR_B',
  31.                                                        'type' => 'device'
  32.                                                      },
  33.                                         'requirement_set' => {
  34.                                                              'exclude_arch' => [
  35.                                                                                'ARCH_E',
  36.                                                                                'ARCH_F'
  37.                                                                              ]
  38.                                                            }
  39.                                       }
  40.                  }
  41.         };
  42.  
The other hash should be exactly the same. I just want to assert with a message and the mismatched key/value if there is any difference found in the hashes.

and Here's what I've been trying to do to compare the hashes:

Expand|Select|Wrap|Line Numbers
  1. sub _compare_hashes :PRIVATE {
  2.  
  3.     my ($data1, $data2) = @_;
  4.     my $found = 0;
  5.     my @missing;
  6.  
  7.     foreach my $parent_node1 (keys %data1) {
  8.         my @childNodes1 = $parent_node1->childNodes();
  9.         foreach $parent_node2 (keys %data2) {
  10.             my @childNodes2 = $parent_node2->childNodes();
  11.             if ($data1{$parent_node1} eq $data2{$parent_node2}) {
  12.                 print "Matched $data1{$parent_node1} & $data2{$parent_node2}\n";
  13.             } else {
  14.                 print "Mismatched $data1{$key1}\n";
  15.                 push @missing, $data1{$key1};
  16.             }
  17.             if ( (scalar(@childNodes1) > 1) || (scalar(@childNodes2) > 1) ) {
  18.                 foreach my $childNode1 (@childNodes1) { 
  19.                     foreach my $childNode2 (@childNodes2) { 
  20.                         _compare_hashes($childnode1, $childnode2)
  21.                     }
  22.                 }
  23.             }
  24.         }
  25.     }
  26. }
  27.  
Jan 30 '09 #12

KevinADC
Expert 2.5K+
P: 4,059
I would look into Data::Compare if you can use modules.

http://search.cpan.org/~dcantrell/Da...ata/Compare.pm
Jan 30 '09 #13

numberwhun
Expert Mod 2.5K+
P: 3,503
@pranksk
See this thread for your answer.

Regards,

Jeff

http://bytes.com/faq.php?faq=how_to_ask_a_question
Jan 30 '09 #14

Post your reply

Sign in to post your reply or Sign up for a free account.