473,385 Members | 1,782 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.



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
13 10229
KevinADC
4,059 Expert 2GB
What have you tried?
Aug 20 '08 #2
You probably need the CPAN module Data::Compare.
Aug 20 '08 #3
nithinpes
410 Expert 256MB
I believe you had posted a similar question sometime back and got a solution for it.
Aug 21 '08 #4
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
3,509 Expert Mod 2GB
@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
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
4,059 Expert 2GB
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
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
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
how do I use code tags so my posts display in a good format?
Jan 30 '09 #11
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
4,059 Expert 2GB
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
3,509 Expert Mod 2GB
@pranksk
See this thread for your answer.

Regards,

Jeff

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

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

Similar topics

4
by: spar | last post by:
I'm converting a Perl script to Python and have run into something I'm not sure how to do in Python. In Perl, I am running through a couple loops and inserting values directly into a complex...
5
by: Jeff | last post by:
I've created a beast! Here is my data structure: $VAR1 = 'bunkers'; $VAR2 = { 'items' => , \ ] }; $VAR3 = 'simpsons'; $VAR4 = {
5
by: John Smith | last post by:
Can someone point me to an example of how to implement and access the kind of object shown below? Most of the examples if found are an object that contains one other object. I need to create an...
6
by: Mahesh Hardikar | last post by:
Hi , Oracle 8.1.7.0.0 on HP-UX 11.0 I am a newbie as far as PL-SQL is concerned . I have Sybase/MSSQL T-SQL background. We have a report which uses a select statement . This select...
1
by: TOI DAY | last post by:
Hi All, This is what I want to do. Support I have two file name abc.txt, xyz.txt I did created md5 hash for abc.txt and store it some directory Here is how I create md5 for the abc.txt ...
5
by: drabee | last post by:
Please help 2 things: 1-I need c# code to compare 2 audio files .or any other .net code 2-code to receive bluetooth file from mobile and save it using .net code
4
by: Lamis | last post by:
Hi, what is the best way to compare 2 haschtables contatining objects. the objects has 2 property, name & value. I need to print out the differences -- LZ
28
by: beach.dk | last post by:
Hi, I'm trying to implement a simple hash algorith called rs_hash in javascript, but I cannot get a correct result. In c the code looks like this:
5
by: S S | last post by:
Hi I have a requirement where I am declaring a map within a class. class abc { map <void*, void*mMap; // I do not pass compare struct here. .... }; Here I am not passing compare function,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.