[Pdx-pm] bit wise comparison

Tom Keller kellert at ohsu.edu
Thu Feb 11 12:22:00 PST 2010


Greetings,
A Bioperl list I'm on gave the following as a fast string comparison method:

#################################
$in = 'ACCTCCTCCTCGAGTATGTG';
$tgt = 'TATCTTGCGCCGGAGATAAT';
$mask = pack("A*",$in)^pack("A*",$tgt);
$matches = $mask =~ tr/"\x0"/"\x0"/;

Impressive! Not often you see pack() let alone exclusive-or with a
scalar context tr// thrown in for good measure!

For those who don't follow what it is doing, here is my (possibly
wrong) interpretation: The pack() is converting each of the two (equal
length) strings into a byte set. A bit-wise exclusive-or (XOR) is
performed between these two byte sets. This will create bytes of value
zero (0) where they were the same, and non-zero where they were
different. The tr// then counts how many of the bytes were zero (\x0
is ascii zero).
#################################

I can't get it to work on my machine. Would someone be able to help me with this?

For one thing, isn't ascii zero "x30" in hex? But even with that change, I get no return value for $matches in:
my $matches = $mask =~ tr/"\x30"/"\x30"/;

thanks,

Tom Keller
MMI DNA Services Core<http://www.ohsu.edu/xd/research/research-cores/dna-analysis/>
4-2442
kellert at ohsu edu<mailto:kellert at ohsu.edu>
RJH (CROET/BasicScience) 6339b





-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/pdx-pm-list/attachments/20100211/023469aa/attachment.html>


More information about the Pdx-pm-list mailing list