Compare two complex Hash

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dillipkumar
    New Member
    • Mar 2008
    • 41

    Compare two complex Hash

    Hi all,

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

    Please Help somebody.

    Thanks
    DK.



    Code:
    %hash1 = (	file1 => [			
    							{
    							  line => '107',
    							  filename => 'SRC',
    							  source => '',
    							  target => [{ 
    													C1 => '023',
    													C2 => 'X',
    												},],
    							},
    							{
    							  line => '21',
    							  filename => 'SRC',
    							  source => '',
    							  target => [{
    													C1 => '139',
    													C2 => '221FIFTH',
    											 },],
    							},
    							{
    							  line => '1',
    							  filename => 'SRC',
    							  source => '',
    							  target => [{
    													C1 => '004',
    													C2 => '101STR',
    											 },],
    							},
    					]
    
       		);
    
    %hash2 = (	file2 => [		{
    							  line => '107',
    							  filename => 'TRG',
    							  target => '',
    							  source => [ 
    												{ 
    													C1 => '023',
    													C2 => 'X',
    												},
    										 ],
    							},
    							{
    							  line => '1',
    							  filename => 'TRG',
    							  target => '',
    							  source => [ 
    												{
    													C1 => '003',
    													C2 => '101STHR',
    												},
    											 ],
    							},
    							{
    							  line_number => '21',
    							  filename => 'TRG',
    							  target => '',
    							  source => [ 
    												{
    													C1 => '1349',
    													C2 => '221FIFTH',
    												},
    											 ],
    							},
    					],
    					);

    Above are 2 hashes

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


    Code:
    %different = (	diff => [			
    							{
    							  line => '1',
    							  filename => 'SRC',
    							  source => '',
    							  target => [{
    													C1 => '004',
    													C2 => '101STR',
    											 },],
    							},
    							{
    							  line => '1',
    							  filename => 'TRG',
    							  source => [ 
    												{
    													C1 => '003',
    													C2 => '101STHR',
    												},
    										],
    							  target => '',
    							},
    							{
    							  line => '21',
    							  filename => 'SRC',
    							  source => '',
    							  target => [{
    													C1 => '139',
    													C2 => '221FIFTH',
    											 },],
    							},
    							{
    							  line => '21',
    							  filename => 'TRG',
    							  source => [{
    													C1 => '139',
    													C2 => '221FIFTH',
    											 },],
    							  target => '',
    							},
    						]
    
       		);
    Last edited by eWish; Aug 20 '08, 11:31 PM. Reason: Please use the code tags
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    What have you tried?

    Comment

    • chaosprime
      New Member
      • Aug 2008
      • 5

      #3
      You probably need the CPAN module Data::Compare.

      Comment

      • nithinpes
        Recognized Expert Contributor
        • Dec 2007
        • 410

        #4
        I believe you had posted a similar question sometime back and got a solution for it.

        Comment

        • pranksk
          New Member
          • Jan 2009
          • 6

          #5
          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!

          Comment

          • numberwhun
            Recognized Expert Moderator Specialist
            • May 2007
            • 3467

            #6
            Originally posted by pranksk
            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!
            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

            Comment

            • pranksk
              New Member
              • Jan 2009
              • 6

              #7
              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

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                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?

                Comment

                • pranksk
                  New Member
                  • Jan 2009
                  • 6

                  #9
                  Here is my hash.

                  Code:
                  $VAR1 = {
                            'files' => {
                                       'requirement_set' => {
                                                            'exclude_arch' => [
                                                                              'ARCH_C',
                                                                              'ARCH_A',
                                                                              'ARCH_E'
                                                                            ]
                                                          },
                                       'name' => [
                                                 'file_1.m',
                                                 'file_2.m'
                                               ]
                                     },
                            'version' => '1.0',
                            'macrodef' => {
                                          'local2' => {
                                                      'exclude_arch' => 'ARCH_F'
                                                    },
                                          'local3' => {
                                                      'exclude_arch' => 'ARCH_F'
                                                    },
                                          'local1' => {
                                                      'exclude_arch' => 'ARCH_E'
                                                    }
                                        },
                            'dir' => {
                                     'requirement_set' => {
                                                          'attribute' => {
                                                                         'content' => 'ATTR_B',
                                                                         'type' => 'device'
                                                                       },
                                                          'requirement_set' => {
                                                                               'exclude_arch' => [
                                                                                                 'ARCH_E',
                                                                                                 'ARCH_F'
                                                                                               ]
                                                                             }
                                                        }
                                   }
                          };
                  My second hash should be the same as this else I want to assert.

                  Comment

                  • pranksk
                    New Member
                    • Jan 2009
                    • 6

                    #10
                    Here's what I've been trying, but i don't think it's working:

                    Code:
                    sub _compare_hashes :PRIVATE {
                        
                        my ($data1, $data2) = @_;
                        my $found = 0;
                        my @missing;
                        
                        foreach my $parent_node1 (keys %data1) {
                            my @childNodes1 = $parent_node1->childNodes();
                            foreach $parent_node2 (keys %data2) {
                                my @childNodes2 = $parent_node2->childNodes();
                                if ($data1{$parent_node1} eq $data2{$parent_node2}) {
                                    print "Matched $data1{$parent_node1} & $data2{$parent_node2}\n";
                                } else {
                                    print "Mismatched $data1{$key1}\n";
                                    push @missing, $data1{$key1};
                                }
                                if ( (scalar(@childNodes1) > 1) || (scalar(@childNodes2) > 1) ) {
                                    foreach my $childNode1 (@childNodes1) { 
                                        foreach my $childNode2 (@childNodes2) { 
                                            _compare_hashes($childnode1, $childnode2)
                                        }
                                    }
                                }
                            }
                        }
                    }
                    Last edited by numberwhun; Jan 30 '09, 08:42 PM. Reason: Please use code tags!

                    Comment

                    • pranksk
                      New Member
                      • Jan 2009
                      • 6

                      #11
                      how do I use code tags so my posts display in a good format?

                      Comment

                      • pranksk
                        New Member
                        • Jan 2009
                        • 6

                        #12
                        Here's my hash again:

                        Code:
                        $VAR1 = {
                                  'files' => {
                                             'requirement_set' => {
                                                                  'exclude_arch' => [
                                                                                    'ARCH_C',
                                                                                    'ARCH_A',
                                                                                    'ARCH_E'
                                                                                  ]
                                                                },
                                             'name' => [
                                                       'file_1.m',
                                                       'file_2.m'
                                                     ]
                                           },
                                  'version' => '1.0',
                                  'macrodef' => {
                                                'local2' => {
                                                            'exclude_arch' => 'ARCH_F'
                                                          },
                                                'local3' => {
                                                            'exclude_arch' => 'ARCH_F'
                                                          },
                                                'local1' => {
                                                            'exclude_arch' => 'ARCH_E'
                                                          }
                                              },
                                  'dir' => {
                                           'requirement_set' => {
                                                                'attribute' => {
                                                                               'content' => 'ATTR_B',
                                                                               'type' => 'device'
                                                                             },
                                                                'requirement_set' => {
                                                                                     'exclude_arch' => [
                                                                                                       'ARCH_E',
                                                                                                       'ARCH_F'
                                                                                                     ]
                                                                                   }
                                                              }
                                         }
                                };
                        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:

                        Code:
                        sub _compare_hashes :PRIVATE {
                            
                            my ($data1, $data2) = @_;
                            my $found = 0;
                            my @missing;
                            
                            foreach my $parent_node1 (keys %data1) {
                                my @childNodes1 = $parent_node1->childNodes();
                                foreach $parent_node2 (keys %data2) {
                                    my @childNodes2 = $parent_node2->childNodes();
                                    if ($data1{$parent_node1} eq $data2{$parent_node2}) {
                                        print "Matched $data1{$parent_node1} & $data2{$parent_node2}\n";
                                    } else {
                                        print "Mismatched $data1{$key1}\n";
                                        push @missing, $data1{$key1};
                                    }
                                    if ( (scalar(@childNodes1) > 1) || (scalar(@childNodes2) > 1) ) {
                                        foreach my $childNode1 (@childNodes1) { 
                                            foreach my $childNode2 (@childNodes2) { 
                                                _compare_hashes($childnode1, $childnode2)
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        Comment

                        • KevinADC
                          Recognized Expert Specialist
                          • Jan 2007
                          • 4092

                          #13
                          I would look into Data::Compare if you can use modules.

                          Comment

                          • numberwhun
                            Recognized Expert Moderator Specialist
                            • May 2007
                            • 3467

                            #14
                            Originally posted by pranksk
                            how do I use code tags so my posts display in a good format?
                            See this thread for your answer.

                            Regards,

                            Jeff

                            Join 420,000+ software developers and IT professionals

                            Comment

                            Working...