in reply to Re^2: Recovering Substrings to String with Gap
in thread Recovering Substrings to String with Gap

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; }