On Wed, Nov 15, 2000 at 12:40:24AM -0500, Scott R. Godin wrote: > on 11/14/2000 11:19 PM, Ronald J Kimball at rjk@linguist.dartmouth.edu > wrote: > > > Yup, you're right, you do need eval to use non-constant strings in tr///. > > But fortunately, you only need the eval to be executed once. For example: > > > > my $reverse = reverse '0' .. '9', 'a'..'z'; > > my $trans = eval "sub { \$_[0] =~ tr/a-z0-9/$reverse/ }"; > > It wouldn't have occurred to me to put the reference where you did.. can you > give me a clue or two as to why it's done that way instead of referencing > the anonymous sub itself? I'm not sure what you mean... That does create a reference to an anonymous sub. Or do you mean the backslash before $_[0]? That's inside double quotes; the backslash escapes the dollar sign. :) > > Then, to avoid so many subroutine dereferences and calls, you can put the > > loop inside the eval. And if you're only going to execute the loop once, > > you don't need the anonymous sub, of course; you can just eval the code > > directly. (See TransSub2 below for an example of that.) > > hmm. I think, as I mention below, that it'd be $trans1 that I would use, > however it occurs to me that you might have yet another trick to use, that > would work in the context of my code. see my previous post to the list for > the url. Not right now, but I might think of something later. :) > > And here's another benchmark, with various eval approaches: > > > > > > Benchmark: timing 8192 iterations of Substitute, Trans, TransEval, > > TransSub1, TransSub2... > > Substitute: 26 wallclock secs (24.58 usr + 0.04 sys = 24.62 CPU) > > Trans: 2 wallclock secs ( 1.30 usr + 0.01 sys = 1.31 CPU) > > TransEval: 4 wallclock secs ( 3.96 usr + 0.01 sys = 3.97 CPU) > > TransSub1: 2 wallclock secs ( 2.08 usr + 0.00 sys = 2.08 CPU) > > TransSub2: 2 wallclock secs ( 1.22 usr + 0.00 sys = 1.22 CPU) > > whew. 'nuff said. what computer are you running there anyway? (just curious) ~> uname -a IRIX linguist 6.5 01221644 IP32 I haven't tried the benchmark on my Mac, I expect it would be somewhat slower. Ronald # ===== Want to unsubscribe from this list? # ===== Send mail with body "unsubscribe" to macperl-request@macperl.org