Clever mapping of the characters in one of the strings could work around the problems with the XOR approach.  For example, using tr/ATCG/HRDZ/:

my %change; # reverse lookup table my @t = qw(A T C G); for my $t1 (@t) { for my $t2 (@t) { my $t = $t2; $t =~ tr/ATCG/HRDZ/; $change{ $t1 ^ $t } = "$t1->$t2"; } } sub diff { my ($target, $str) = @_; $str =~ tr/ATCG/HRDZ/; my $diff = $target ^ $str; while ($diff =~ /([^\x09\x06\x07\x1d])/g) { printf " %d: %s\n", pos($diff), $change{$1}; } } my $target = "ATTCCGGG"; for (qw(ATTGCGGG ATACCGGC)) { print "\n$target\n$_\n"; diff($target, $_); } __END__ ATTCCGGG ATTGCGGG 4: C->G ATTCCGGG ATACCGGC 3: T->A 8: G->C

The reverse lookup table just needs to be set up once, and the remaining operations (string bit operation, tr///, m//g) should all be pretty fast.

(As the keys in the lookup table are integers < 256, you could in theory also set up an array, and use the xor value as the index.)


In reply to Re: mismatching characters in dna sequence by Eliya
in thread mismatching characters in dna sequence by prbndr

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.