[Pdx-pm] bit wise comparison

Ben Prew ben.prew at gmail.com
Thu Feb 11 16:10:03 PST 2010


Here's the original thread, it looks like the author was having
problems getting it working without the pack under Activestate 5.10

http://old.nabble.com/string-comparision-mismatches-and-matches-td27436645.html


On Thu, Feb 11, 2010 at 1:46 PM, Steve Cayford <scfrd1 at gmail.com> wrote:
> As far as I can tell, the pack commands are superfluous. They're just saying
> take this string and pack it into a string.
>
> This works fine with the original strings...
>
> print scalar(
>    ("ACCTCCTCCTCGAGTATGTG" ^ "TATCTTGCGCCGGAGATAAT") =~ tr/\x0/\x0/
>  ), "\n";
>
> The tr/\x0/\x0/ is transliterating bytes that have zeroed out bits, not
> ASCII zeros.
>
> -Steve
>
> Tom Keller wrote:
>>
>> 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
>>
>>
> _______________________________________________
> Pdx-pm-list mailing list
> Pdx-pm-list at pm.org
> http://mail.pm.org/mailman/listinfo/pdx-pm-list
>



-- 
--Ben


More information about the Pdx-pm-list mailing list