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.
|