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.


In reply to Re: question 'string' by AnomalousMonk
in thread question 'string' by nicholaspr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.