CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Comparining two array of hashesh using Data::Compare module

 



subhesh
New User

May 19, 2015, 1:53 AM

Post #1 of 2 (6332 views)
Comparining two array of hashesh using Data::Compare module Can't Post

Hi,

I am trying to compare two array of hashes using perl module Data::Compare. Comparison is working well when array elements are defined in the same sequences in both the arrays.

But comparison is failing when array elements are shuffled.

My Script::
==============

Code
#!/usr/bin/perl 

use Data::Compare;

my $expected = [
{
'address' => '4.0.0.12',
'family' => 'inet4',
'gateway' => '4.0.0.1',
'hostname' => 'HostRecUT_Case14_data4_2',
'interface' => 'e1b',
'mtu' => '1500',
'netid' => 'inet4_data4_2',
'netmask' => '255.255.0.0',
'nettype' => 'data',
'vlan_id' => '400',
'vlan_tagged' => '1',
},
{
'address' => '4.0.0.11',
'family' => 'inet4',
'gateway' => '4.0.0.1',
'hostname' => 'HostRecUT_Case14_data4_1',
'interface' => 'e1a',
'mtu' => '1500',
'netid' => 'inet4_data4_1',
'netmask' => '255.255.0.0',
'nettype' => 'data',
'vlan_id' => '400',
'vlan_tagged' => '1',
},
{
'address' => 'fd20:8b1e:b255:0004:0000:0000:0000:0011',
'family' => 'inet6',
'gateway' => 'fd20:8b1e:b255:0004:0000:0000:0000:0001',
'hostname' => 'HostRecUT_Case14_data4_1-v6',
'interface' => 'e1a',
'mtu' => '1500',
'netid' => 'inet6_data4_1',
'netmask' => '64',
'nettype' => 'data',
'vlan_id' => '400',
'vlan_tagged' => '1',
},
{
'address' => 'fd20:8b1e:b255:0004:0000:0000:0000:0012',
'family' => 'inet6',
'gateway' => 'fd20:8b1e:b255:0004:0000:0000:0000:0001',
'hostname' => 'HostRecUT_Case14_data4_2-v6',
'interface' => 'e1b',
'mtu' => '1500',
'netid' => 'inet6_data4_2',
'netmask' => '64',
'nettype' => 'data',
'vlan_id' => '400',
'vlan_tagged' => '1',
}
];
my $actual = [

{
'address' => '4.0.0.11',
'family' => 'inet4',
'gateway' => '4.0.0.1',
'hostname' => 'HostRecUT_Case14_data4_1',
'interface' => 'e1a',
'mtu' => '1500',
'netid' => 'inet4_data4_1',
'netmask' => '255.255.0.0',
'nettype' => 'data',
'vlan_id' => '400',
'vlan_tagged' => '1',
},
{
'address' => '4.0.0.12',
'family' => 'inet4',
'gateway' => '4.0.0.1',
'hostname' => 'HostRecUT_Case14_data4_2',
'interface' => 'e1b',
'mtu' => '1500',
'netid' => 'inet4_data4_2',
'netmask' => '255.255.0.0',
'nettype' => 'data',
'vlan_id' => '400',
'vlan_tagged' => '1',
},
{
'address' => 'fd20:8b1e:b255:0004:0000:0000:0000:0012',
'family' => 'inet6',
'gateway' => 'fd20:8b1e:b255:0004:0000:0000:0000:0001',
'hostname' => 'HostRecUT_Case14_data4_2-v6',
'interface' => 'e1b',
'mtu' => '1500',
'netid' => 'inet6_data4_2',
'netmask' => '64',
'nettype' => 'data',
'vlan_id' => '400',
'vlan_tagged' => '1',
},
{
'address' => 'fd20:8b1e:b255:0004:0000:0000:0000:0011',
'family' => 'inet6',
'gateway' => 'fd20:8b1e:b255:0004:0000:0000:0000:0001',
'hostname' => 'HostRecUT_Case14_data4_1-v6',
'interface' => 'e1a',
'mtu' => '1500',
'netid' => 'inet6_data4_1',
'netmask' => '64',
'nettype' => 'data',
'vlan_id' => '400',
'vlan_tagged' => '1',
}
];

if ( !Compare($actual, $expected ) )
{
print "NOT MATCHED\n";
}
else{
print "MATCHED\n";
}

========================

Output: NOT MATCHED


Can anyone please suggest a way other that comparing these two array of hashes without comparing each and every element of array.


(This post was edited by FishMonger on May 19, 2015, 8:00 AM)


FishMonger
Veteran / Moderator

May 19, 2015, 8:58 AM

Post #2 of 2 (6323 views)
Re: [subhesh] Comparining two array of hashesh using Data::Compare module [In reply to] Can't Post

Are you wanting to compare the structures or the contents?

Both of the arrays have the same structure, but since the ordering of the array elements differ, they won't match. You need to sort the arrays then do the compare.


Code
#!/usr/bin/perl 

use strict;
use warnings;
use Data::Compare;
use Data::Dumper;

my $expected = [
{
'address' => '4.0.0.12',
'family' => 'inet4',
'gateway' => '4.0.0.1',
'hostname' => 'HostRecUT_Case14_data4_2',
'interface' => 'e1b',
'mtu' => '1500',
'netid' => 'inet4_data4_2',
'netmask' => '255.255.0.0',
'nettype' => 'data',
'vlan_id' => '400',
'vlan_tagged' => '1',
},
{
'address' => '4.0.0.11',
'family' => 'inet4',
'gateway' => '4.0.0.1',
'hostname' => 'HostRecUT_Case14_data4_1',
'interface' => 'e1a',
'mtu' => '1500',
'netid' => 'inet4_data4_1',
'netmask' => '255.255.0.0',
'nettype' => 'data',
'vlan_id' => '400',
'vlan_tagged' => '1',
},
{
'address' => 'fd20:8b1e:b255:0004:0000:0000:0000:0011',
'family' => 'inet6',
'gateway' => 'fd20:8b1e:b255:0004:0000:0000:0000:0001',
'hostname' => 'HostRecUT_Case14_data4_1-v6',
'interface' => 'e1a',
'mtu' => '1500',
'netid' => 'inet6_data4_1',
'netmask' => '64',
'nettype' => 'data',
'vlan_id' => '400',
'vlan_tagged' => '1',
},
{
'address' => 'fd20:8b1e:b255:0004:0000:0000:0000:0012',
'family' => 'inet6',
'gateway' => 'fd20:8b1e:b255:0004:0000:0000:0000:0001',
'hostname' => 'HostRecUT_Case14_data4_2-v6',
'interface' => 'e1b',
'mtu' => '1500',
'netid' => 'inet6_data4_2',
'netmask' => '64',
'nettype' => 'data',
'vlan_id' => '400',
'vlan_tagged' => '1',
}
];

my $actual = [

{
'address' => '4.0.0.11',
'family' => 'inet4',
'gateway' => '4.0.0.1',
'hostname' => 'HostRecUT_Case14_data4_1',
'interface' => 'e1a',
'mtu' => '1500',
'netid' => 'inet4_data4_1',
'netmask' => '255.255.0.0',
'nettype' => 'data',
'vlan_id' => '400',
'vlan_tagged' => '1',
},
{
'address' => '4.0.0.12',
'family' => 'inet4',
'gateway' => '4.0.0.1',
'hostname' => 'HostRecUT_Case14_data4_2',
'interface' => 'e1b',
'mtu' => '1500',
'netid' => 'inet4_data4_2',
'netmask' => '255.255.0.0',
'nettype' => 'data',
'vlan_id' => '400',
'vlan_tagged' => '1',
},
{
'address' => 'fd20:8b1e:b255:0004:0000:0000:0000:0012',
'family' => 'inet6',
'gateway' => 'fd20:8b1e:b255:0004:0000:0000:0000:0001',
'hostname' => 'HostRecUT_Case14_data4_2-v6',
'interface' => 'e1b',
'mtu' => '1500',
'netid' => 'inet6_data4_2',
'netmask' => '64',
'nettype' => 'data',
'vlan_id' => '400',
'vlan_tagged' => '1',
},
{
'address' => 'fd20:8b1e:b255:0004:0000:0000:0000:0011',
'family' => 'inet6',
'gateway' => 'fd20:8b1e:b255:0004:0000:0000:0000:0001',
'hostname' => 'HostRecUT_Case14_data4_1-v6',
'interface' => 'e1a',
'mtu' => '1500',
'netid' => 'inet6_data4_1',
'netmask' => '64',
'nettype' => 'data',
'vlan_id' => '400',
'vlan_tagged' => '1',
}
];

@$expected = sort { $a->{'address'} cmp $b->{'address'} } @$expected;
@$actual = sort { $a->{'address'} cmp $b->{'address'} } @$actual;
print Dumper $expected, $actual;


if ( !Compare($actual, $expected ) ) {
print "NOT MATCHED\n";
}
else {
print "MATCHED\n";
}



(This post was edited by FishMonger on May 19, 2015, 8:59 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives