Finding characters where two string differs can be done with bitwise operations. If you binary XOR two strings, positions where both characters are the same come out as a null byte. When doing several comparisons, one can accumulate the differing positions using binary OR:
use warnings; use strict; use 5.010; # for say() my $a='AAATGCCTT'; my $b='AAAAGCGTC'; my $c='AAAGGCGTC'; my $mask = chr(0) x length $a; for ($b, $c) { $mask |= $a ^ $_; } # just to illustrate what the mask looks like: use Data::Dumper; $Data::Dumper::Useqq = 1; # count number of 0-bytes my $matches =()= $mask =~ /\0/g; say "Matches: ", $matches; say "Non-matches: ", length($a) - $matches;
This approach should scale well for longer strings, since the binary operations are faster than looping over all characters.
In reply to Re: Is it possible to find the number of matching and non-matching positions in strings using perl code?
by moritz
in thread Is it possible to find the number of matching and non-matching positions in strings using perl code?
by supriyoch_2008
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |