How do I compare two hashes in Perl without using Data::Compare?
Test::Deep::NoTest has the same functionality.
Quick, dirty, and I'm sure not that efficient:
use strict;
use warnings;
use Data::Dumper;
sub compare ($$) {
local $Data::Dumper::Terse = 1;
local $Data::Dumper::Indent = 0;
Dumper(shift) eq Dumper(shift);
}
my %a = ( foo => 'bar', bar => [ 0 .. 3 ] );
my %b = ( foo => 'bar', bar => [ 0 .. 3 ] );
my %c = ( foo => 'bar', bar => [ 0 .. 4 ] );
print Dumper compare \%a, \%b;
print Dumper compare \%a, \%c;
Compare is not a detailed enough phrase when talking about hashes. There are many ways to compare hashes:
Do they have the same number of keys?
if (%a == %b) {
print "they have the same number of keys\n";
} else {
print "they don't have the same number of keys\n";
}
Are the keys the same in both hashes?
if (%a != %b) {
print "they don't have the same number of keys\n";
} else {
my %cmp = map { $_ => 1 } keys %a;
for my $key (keys %b) {
last unless exists $cmp{$key};
delete $cmp{$key};
}
if (%cmp) {
print "they don't have the same keys\n";
} else {
print "they have the same keys\n";
}
}
Do they have the same keys and the same values in both hashes?
if (%a != %b) {
print "they don't have the same number of keys\n";
} else {
my %cmp = map { $_ => 1 } keys %a;
for my $key (keys %b) {
last unless exists $cmp{$key};
last unless $a{$key} eq $b{$key};
delete $cmp{$key};
}
if (%cmp) {
print "they don't have the same keys or values\n";
} else {
print "they have the same keys or values\n";
}
}
Are they isomorphic (I will leave this one up to the reader as I don't particularly want to try implementing it from scratch)?
Or some other measure of equal?
And, of course, this code only deals with simple hashes. Adding complex data structures makes it even more complex.
See How do I test whether two arrays or hashes are equal?
Perl's FAQ and answers are part of you Perl distribution. You can view the version of this answer that came with your perl
by running:
$ perldoc -q equal
in your terminal.
@zakovyrya See this answer: https://stackoverflow.com/a/2011443/2606517 The keys ordering is from an internal datastructure.
For comparing:
sub HashCompare {
my ( $a, $b ) = @_;
my %rhash_1 = %$a;
my %rhash_2 = %$b;
my $key = undef;
my $hash_2_line = undef;
my $hash_1_line = undef;
foreach $key ( keys(%rhash_2) ) {
if ( exists( $rhash_1{$key} ) ) {
if ( $rhash_1{$key} ne $rhash_2{$key} ) {
print "key $key in $file_1 = $rhash_1{$key} & $rhash_2{$key} in $file_2\n";
}
}
}
else {
print "key $key in $file_1 is not present in $file_2\n";
#next;
}
}
foreach my $comp_key ( keys %rhash_1 ) {
if ( !exists( $rhash_2{$comp_key} ) ) {
print MYFILE "key $comp_key in $file_2 is not present in $file_1\n";
}
}
return;
}
Creating hash with no duplicate keys:
sub CreateHash {
my (@key_val_file ) = @_;
my $key_count = 1;
my %hash_key_val = ();
my $str4 = undef;
local $/ = undef;
foreach my $each_line (@key_val_file) {
@key_val = split( /,/, $each_line );
if ( exists( $hash_key_val{$key_val[0]} ) ) {
$key_count = $key_count + 1;
$str4 = $key_val[0] . " occurence-" . $key_count;
$hash_key_val{$str4} = $key_val[1];
}
else {
$hash_key_val{$key_name} = $key_val[1];
}
}
}
$key_count = 1;
close FILE;
return %hash_key_val;
}