Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Identifying Overlapping Area in a Set of Strings

by reasonablekeith (Deacon)
on Jul 29, 2005 at 09:31 UTC ( [id://479319]=note: print w/replies, xml ) Need Help??


in reply to Identifying Overlapping Area in a Set of Strings

Well the following replaces the front of the strings as per your spec, I couldn't be arsed to do the end of the strings, as you need to know in advance where the next string starts (and if its before the endpoint of your current string). If might be best to search first and save the start/end positions of the substrings first and then go through replacing values.

This should be a good start though.

#!/perl -w use strict; my $string = "abcdefghijklmnopqrstuvwxyz"; my @sub_strings = ('cdef', 'efghij', 'klmno', 'mnopqrst'); my $string_index = 0; my $last_end_point = 0; foreach my $sub_string (@sub_strings) { my $string_index = index($string, $sub_string, $string_index); die ("Couldn't find string") if $string_index == -1; my $overlap = $last_end_point - $string_index; if ($overlap > 0) { substr($sub_string, 0, $overlap) = '-'x$overlap; } $last_end_point = $string_index + (length($sub_string)); } print join(',', @sub_strings);
update: removed stupid variable declaration
---
my name's not Keith, and I'm not reasonable.

Replies are listed 'Best First'.
Re^2: Identifying Overlapping Area in a Set of Strings
by reasonablekeith (Deacon) on Jul 29, 2005 at 10:31 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://479319]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-18 20:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found