If I correctly understand your problem, you want to find cases where two complete values are the same (the things separated by "~~"), not just any old substring.

To find "a value I've seen before", the quickest way to do it is usually to have that value set as the key of a hash; in this case you additionally want it keyed on the ID, so you probably want to build a hash-of-hashes structure something like:

my %values_by_id; # for each line from the first file: my($id1, $values_string1) = split /:/, $line1; for my $value1 (split /~~/, $values_string1) { # set each unique value to be true in the hash $values_by_id{$id1}{$value1} = 1; } # and then for a line from the second file: my($id2, $values_string2) = split /:/, $line2; my @shared_values; for my $value2 (split /~~/, $values_string2) { push @shared_values, $value2 if $values_by_id{$id2}{$values2}; }

Hope this helps.


In reply to Re: Find common substrings by hv
in thread Find common substrings 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.