in reply to Recovering Substrings to String with Gap

it's because you always search the substring from the beginning instead of from the point after the last match.
my $after = 0; foreach my $sbstr ( @$array ) { my $pos = index $str, $sbstr, $after; substr ($nstring, $pos, length ($sbstr)) = $sbstr; $after=$pos+length($sbstr); }

Replies are listed 'Best First'.
Re^2: Recovering Substrings to String with Gap
by monkfan (Curate) on Apr 16, 2005 at 11:14 UTC
    No it wont' do, cause it will "over append" those overlapping substrings (@arr2 and @arr3) which gives:
    GATTACGNNNNNNNNNCGTGTAANNNNTTACGAG NNNTACGAGTNNNGCTCGTGNNNNNNNGTGGCGC
    Instead of OP (already) correct answers.
    Regards,
    Edward
      oops, you are right, I had supposed that the strings didn't overlap...

      but if the substrings are sorted by their position on the original string, it's easy to solve, just make $after=$pos+1 or:

      my $pos=0; foreach my $sbstr ( @$array ) { $pos = index $str, $sbstr, $pos; substr ($nstring, $pos, length ($sbstr)) = $sbstr; $pos++ }
      and if they are not sorted, well, then you can use some hash to remember the last $pos for a given $sbstr and start searching after that...
      my %after; foreach my $sbstr ( @$array ) { my $pos = index $str, $sbstr, $after{$sbstr}||0; substr ($nstring, $pos, length ($sbstr)) = $sbstr; $after{$sbstr}=$pos+1; }