[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Unexpected behavior from tr function



On Wed, 13 Oct 1999 01:59:00 -0500, Richard L. Anderson wrote:

>	print "Frequency of $_ in $test: " . $test=~tr/$_/$_/ . "\n";

tr/// doesn't interpolate. Yup, silly, but that's the way it is. So, it
counts (BTW you don't need the right hand side) occurences of '$' and of
'_'.

One way around it would be:

	eval "\$test =~ tr/$_//";

but that will be quite a bit slower.

Oh well. Maybe you should avoid tr/// altogether, and do

	print scalar(() = $test =~ /$a/g); # -> 7


I hear that // and s/// are reasonably to very fast alternatives for
tr/// for many uses. For example:

	tr/ \r\n\t/ /s;

might well be replaced by

	s/[ \r\n\t]+/ /g;

-- 
	Bart.

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org