Here's another method using map & grep functions. I believe this should capture all the data you need.

It only performs a single pass through the hash keys. There are no nested loops.

# hash_key_cmp use strict; use warnings; # Test data our %CVS = ( b => 1, c => 0, d => 1, e => 1, f => 1, g => 0, h => 1, i => 1, j => 1, k => 1, l => 1, ); our %SQL = ( x_a => 1, x_b => 1, x_d => 1, x_e => 1, x_f => 0, x_g => 1, x_h => 0, x_i => 1, x_j => 1, x_l => 1, x__k => 1, x_m => 0, ); # Compare hashes (my $prefix = (each %SQL)[0]) =~ s/^(.+_)?.*/$1/; my $prefix_len = length $prefix; my (@valid_in_both, @bad_in_both, @not_in_sql, @not_in_cvs); map { exists $SQL{join '', $prefix, $_} ? $CVS{$_} eq $SQL{join '', $prefix, $_} ? push(@valid_in_both, $_) : push(@bad_in_both, $_) : push(@not_in_sql, $_) } keys %CVS; @not_in_cvs = grep { ! exists($CVS{substr $_, $prefix_len}) } keys %SQ +L; # Output results print 'Not in SQL: ', "@not_in_sql", "\n"; print 'Not in CVS: ', "@not_in_cvs", "\n"; print 'Valid in both: ', "@valid_in_both", "\n"; print 'Bad in both: ', "@bad_in_both", "\n";

Here's the output:

[ ~/tmp ] $ perl hash_key_cmp Not in SQL: c k Not in CVS: x_a x__k x_m Valid in both: b d e i j l Bad in both: f g h [ ~/tmp ] $

Update: Changed second map to a grep. Line used to read:

map { exists($CVS{substr $_, $prefix_len}) || push(@not_in_cvs, $_) } +keys %SQL;

Brain stuck in push() mode, I think :-)

Regards,

PN5


In reply to Re: Comparing hash data by Prior Nacre V
in thread Comparing hash data by FubarPA

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.