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.

In reply to Re: comparing key-value pairs of two hashes by polettix
in thread comparing key-value pairs of two hashes by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.