Peter Scott wrote:
In article <[EMAIL PROTECTED]>,
 [EMAIL PROTECTED] (Edward Wijaya) writes:

BTW, can you explain what's the difference between these two?

sub hamming_distance_string { ( $_[0] ^ $_[1] ) =~ tr/\0// }

In a scalar context, tr/// returns the count of the number of characters seen.

sub hamming_distance_string { () = ( $_[0] ^ $_[1] ) =~ /\0/g }

From inside to out: make the global match on the XOR result.
We want this to be in a list context so that all of them are made,
but we don't need there to be anything in the list assigned, so
we use the empty list (). This subroutine must be called in a
scalar context, because the result of a list assignment in scalar context is the number of things assigned, i.e., the
number of matches. Elegant, but... somewhat advanced.

Yes, you're right, I should have assigned to a scalar to avoid any confusion about the calling context. :-)


sub hamming_distance_string { my $ret = ( $_[0] ^ $_[1] ) =~ tr/\0//; $ret }

sub hamming_distance_string { my $ret = () = ( $_[0] ^ $_[1] ) =~ /\0/g; $ret }




John -- use Perl; program fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to