in reply to comparing key-value pairs of two hashes
Warning: All code untestedYour code for keys is incomplete, because it does not tell you which keys in %old_gp are not in %new_gp. Starting from blazar's suggestion, I would expand:
More generally:my @diff = grep { !exists $old_gp{$_} } keys %new_gp; push @diff, grep { !exists $new_gp{$_} } keys %old_gp;
More compactly (but not necessarily more efficient or readable, it's a matter of taste):my (%foo, %bar); # Populate %foo and %bar # Keys present in only one hash my @only_in_foo = grep { ! exists $bar{$_} } keys %foo; my @only_in_bar = grep { ! exists $foo{$_} } keys %bar; # Matching keys which have differences in values my @kv_different = map { if (exists $bar{$_} && $bar{$_} ne $foo{$_}) { $_ } else { () } } keys %foo; # Matching keys and values my @kv_equal = map { if (exists $bar{$_} && $bar{$_} eq $foo{$_}) { $_ } else { () } } keys %foo;
my (@only_in_foo, @kv_different, @kv_equal); foreach (keys %foo) { if (exists $bar{$_}) { if ($bar{$_} eq $foo{$_}) { push @kv_equal, $_ } else { push @kv_different, $_ } } else { push @only_in_foo, $_ } } my @only_in_bar = grep { ! exists $foo{$_} } keys %bar;
Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: comparing key-value pairs of two hashes
by Codon (Friar) on Jul 22, 2005 at 16:21 UTC |