my $pos=0; foreach my $sbstr ( @$array ) { $pos = index $str, $sbstr, $pos; substr ($nstring, $pos, length ($sbstr)) = $sbstr; $pos++ } #### my %after; foreach my $sbstr ( @$array ) { my $pos = index $str, $sbstr, $after{$sbstr}||0; substr ($nstring, $pos, length ($sbstr)) = $sbstr; $after{$sbstr}=$pos+1; }