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: Edit Log



subhesh
New User

May 19, 2015, 1:53 AM


Views: 9849
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)


Edit Log:
Post edited by FishMonger (Veteran) on May 19, 2015, 8:00 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