[Pdx-pm] bit wise comparison

Eric Wilhelm enobacon at gmail.com
Thu Feb 11 13:35:31 PST 2010

# from Tom Keller
# on Thursday 11 February 2010 12:22:

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

I get warnings from the quotes (") in the tr//.  You *did* enable 
warnings, right?  :-D

An interesting, but slower trick would let you know where the matches 
are in a more straightforward way.

  $tgt =~ s/(.)/(?:($1)|.)/g;
  my @matches = $in =~ m/^$tgt/;

But why pack() ASCII characters into "A*" ASCII characters?  You should 
get a same and faster result with:

  my $mask = $in ^ $tgt;
  my $matches = $mask =~ tr/\x0//;

"You can't win. You can't break even. You can't quit."
--Ginsberg's Restatement of the Three Laws of Thermodynamics

More information about the Pdx-pm-list mailing list