in reply to question 'string'

As almut wrote, a lot depends on just what you mean by 'delete'; the Devil is in the details.

But in general, this sort of thing is generally handled 'without a for loop' by bitwise boolean operations on strings (see examples below). BrowserUk is very good on this topic (as on so much else), and I seem to remember him or her addressing a similar question in the last month or two, but I can't put my finger on the node at the moment; look back through BrowserUk's posts and you should find much of interest on this subject.

Examples (these are by no means intended to represent the most efficient approaches to these problems!):

>perl -wMstrict -le "my $seq1 = '--TAGAG--T'; my $seq2 = '-ATTGAGATT'; print 'question 1'; (my $mask1 = $seq1) =~ tr{-ATGC}{\x00\xff}; print qq{seq1 '$seq1'}; print qq{seq2 '$seq2'}; $seq2 &= $mask1; $seq2 =~ tr{\x00}{-}; print qq{seq2 '$seq2'}; print 'question 2'; $seq1 = 'G-TATAG'; $seq2 = 'GATCT-G'; print qq{seq1 '$seq1'}; print qq{seq2 '$seq2'}; ( $mask1 = $seq1) =~ tr{-ATGC}{\x00\xff}; (my $mask2 = $seq2) =~ tr{-ATGC}{\x00\xff}; my $dmask = $mask1 & $mask2; $seq1 &= $dmask; $seq2 &= $dmask; my $diff = $seq1 ^ $seq2; $diff =~ tr{\x00-\xff}{=D}; print qq{diff '$diff'}; " question 1 seq1 '--TAGAG--T' seq2 '-ATTGAGATT' seq2 '--TTGAG--T' question 2 seq1 'G-TATAG' seq2 'GATCT-G' diff '===D==='

Update: Slightly improved example for Question 1.