in reply to Is it possible to find the number of matching and non-matching positions in strings using perl code?

I would split the strings into character arrays and then count the matches returned by the each_array function of the module List::MoreUtils. Nomatches is the length of the string minus number of matches.

If efficiency is an issue, the substr method already posted is probably better. Profile to be sure.

  • Comment on Re: Is it possible to find the number of matching and non-matching positions in strings using perl code?

Replies are listed 'Best First'.
Re^2: Is it possible to find the number of matching and non-matching positions in strings using perl code?
by sauoq (Abbot) on May 11, 2012 at 01:16 UTC
    I would split the strings into character arrays and then count the matches returned by the each_array function of the module List::MoreUtils.

    Eeek! No... don't do that. And, for that matter, don't use the approach I gave above either. I just wanted to show how, yes, it could easily be done just by automating the way you might do it by hand.

    If you want efficiency, resort to bit twiddling!

    Like this:

    #!/usr/bin/perl my ($a, $b, $c) = qw (AAATGCCTT AAAAGCGTC AAAGGCGTC); my $bits = ($a ^ $b) | ($b ^ $c); my $c = $bits =~ tr/\0/\0/; print "Similar: $c\n";

    :-)

    -sauoq
    "My two cents aren't worth a dime.";