
FishMonger
Veteran
/ Moderator
May 19, 2015, 8:58 AM
Post #2 of 2
(8072 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.
#!/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)
|