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


Views: 9526
Comparining two array of hashesh using Data::Compare module

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


Views: 9517
Re: [subhesh] Comparining two array of hashesh using Data::Compare module

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)