|
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 => '',
-
},
-
]
-
-
);
| |
Share:
|
You probably need the CPAN module Data::Compare.
| | Expert 256MB |
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!
| | 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
| | |
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
| | 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?
| | | -
$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)
-
}
-
}
-
}
-
}
-
}
-
}
-
| | Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
4 posts
views
Thread by spar |
last post: by
|
5 posts
views
Thread by Jeff |
last post: by
|
5 posts
views
Thread by John Smith |
last post: by
|
6 posts
views
Thread by Mahesh Hardikar |
last post: by
|
1 post
views
Thread by TOI DAY |
last post: by
|
5 posts
views
Thread by drabee |
last post: by
|
4 posts
views
Thread by Lamis |
last post: by
|
28 posts
views
Thread by beach.dk@gmail.com |
last post: by
|
5 posts
views
Thread by S S |
last post: by
| | | | | | | | | | |