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...: Re: [subhesh] Comparining two array of hashesh using Data::Compare module: Edit Log



FishMonger
Veteran / Moderator

May 19, 2015, 8:58 AM


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


Edit Log:
Post edited by FishMonger (Veteran) 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