with inspiration from rnahi i've updated my script to do all that you want. It ought to be a bit quicker that rnahi's just because of the use if index over regular expressions.

Anyway a BIG caveat to all this is that these result are ambiguous if there could be multiple matches for a given substring. For example searching for "GG" twice in "GGGG" could give you.

 ORG:  "GGGG";
 SUB   "GG"
         "GG"; # no overlapping
 or
       "--"
       "--";  # full overlapping
so depending on whether you start searching for the second GG _after_ the end of the first GG, (ie from the third character onwards) or from the _beginning_ of first match (first character) you get entirely different results, both of which are correct.

It's hard to say which is best, you need to look more closely at what you're trying to acheve.

regardless my updated code is as follows...

#!/perl -w use strict; my $string = "abcdefghijklmnopqrstuvwxyz"; my @sub_strings = ('cdef', 'efghij', 'klmno', 'mnopqrst'); my $string_index = 0; my $last_end_point = index($string, $sub_strings[0]) + length($sub_str +ings[0]); foreach my $sub_array_index (1..$#sub_strings) { my $string_index = index($string, $sub_strings[$sub_array_index], +$string_index); my $overlap = $last_end_point - $string_index; # if there's an overlap replace from the front of current string, +and the # end of the previous string if ($overlap > 0) { substr($sub_strings[$sub_array_index], 0, $overlap) = '-'x$ove +rlap; substr($sub_strings[$sub_array_index-1], -$overlap, $overlap) += '-'x$overlap; } $last_end_point = $string_index + length($sub_strings[$sub_array_i +ndex]); }
updated mahi->rnahi after tlm's comments
---
my name's not Keith, and I'm not reasonable.

In reply to Re^2: Identifying Overlapping Area in a Set of Strings by reasonablekeith
in thread Identifying Overlapping Area in a Set of Strings by monkfan

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.