Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2024-03-28 12:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found