Here's my solution, probably a little more complex than Marshall's above.

It does two passes over the hash. In the first pass, it builds a HoA where the keys are the all the words from 'yourStr' and the values are arrays holding the IDs concerned, which ends up looking like this:

good => [ 'ID3' ], hello => [ 'ID1', 'ID3', 'ID2' ], goodbye => [ 'ID1' ], where => [ 'ID1' ]

On the second pass, it checks if the value of 'myStr' is in the HoA, and prints out any matches, excluding 'internal' matches (ie if for the same ID 'hello' were to appear in both 'myStr' and 'yourStr').

use strict; use warnings; my $h = { ID1 => { myStr => 'hello', description => 'Description 1', yourStr => [ 'goodbye', 'where', 'hello'] }, ID2 => { myStr => 'good', description => 'Description 2', yourStr => [ 'hello', ] }, ID3 => { myStr => 'testvar', description => 'Description 2', yourStr => [ 'hello', 'good' ] }, }; my %yourstr; for my $id ( keys %$h ) { for ( @{ $h->{$id}->{yourStr} } ) { push @{ $yourstr{$_} }, $id; } } for my $id ( keys %$h ) { my $mystr = $h->{$id}->{myStr}; if ( $yourstr{$mystr} ) { my @matches = grep { $_ ne $id } @{ $yourstr{$mystr} }; print "'$mystr' in $id matches @matches\n"; } }

Output:

'hello' in ID1 matches ID3 ID2 'good' in 1D2 matches ID3

In reply to Re: Comparing values in same hash? by Not_a_Number
in thread Comparing values in same hash? by legendx

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.