Hi all,
i have two hashes. i need 2 compare these hases & Results are stored in %different hash.
Please Help somebody.
Thanks
DK. - %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 - %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 => '',
-
},
-
]
-
-
);
13 10229
You probably need the CPAN module Data::Compare.
I believe you had posted a similar question sometime back and got a solution for it.
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!
@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
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
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?
-
$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.
-
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)
-
}
-
}
-
}
-
}
-
}
-
}
-
how do I use code tags so my posts display in a good format?
-
$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: -
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)
-
}
-
}
-
}
-
}
-
}
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
by: Jeff |
last post by:
I've created a beast! Here is my data structure:
$VAR1 = 'bunkers';
$VAR2 = {
'items' => ,
\
]
};
$VAR3 = 'simpsons';
$VAR4 = {
|
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...
|
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...
|
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
...
|
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
|
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
|
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:
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |