in reply to comparing key-value pairs of two hashes

Warning: All code untested
Your 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:
my @diff = grep { !exists $old_gp{$_} } keys %new_gp; push @diff, grep { !exists $new_gp{$_} } keys %old_gp;
More generally:
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;
More compactly (but not necessarily more efficient or readable, it's a matter of taste):
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

Don't fool yourself.

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
    For those working on their golf (or who just like some of the fun things that Perl can do), here's your exact code in 3 (Perl) lines.
    my (@only_in_foo, @kv_different, @kv_equal); push( @{ exists $bar{$_} ? $bar{$_} eq $foo{$_} ? \@kv_equal : \@kv_different : \@only_in_foo}, $_) for keys %foo; my @only_in_bar = grep { ! exists $foo{$_} } keys %bar;
    Ivan Heffner
    Sr. Software Engineer, DAS Lead
    WhitePages.com, Inc.