If you need to use a runtime-defined tr/// on more than one line, re-evaling it for each line gets very expensive. It's better to eval a subroutine into existance and use that:
#! perl -slw use strict; sub makeTR{ return eval "sub{ \$_[ 0 ] =~ tr[$_[0]][$_[1]] }"; } my $myTR = makeTR( 'abcd', '1234' ); for my $str ( qw[ abcd aabbccdd xx axbxcxdx ] ) { my $copy = $str; my $count = $myTR->( $copy ); print "Before: $str After: $copy count: $count"; } __END__ c:\test>junk62 Before: abcd After: 1234 count: 4 Before: aabbccdd After: 11223344 count: 8 Before: xx After: xx count: 0 Before: axbxcxdx After: 1x2x3x4x count: 4
In reply to Re: doing tr/// on-the-fly?
by BrowserUk
in thread doing tr/// on-the-fly?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |