If you want to use the same transliteration more than once, you could try evalling it to a sub like this:
This way, you can use the same transliteration any number of times and compile it only once.my $tr = "/a-z/n-za-m/"; my $trs = eval qq{sub {\$_[0]=~tr$tr}}; $s = "uryyb, jbeyq\n"; &$trs($s); print $s;
You can even cache multiple tr patterns like this:
{ my %trd; sub trd { my $tr = $_[0]; $trd{$tr} ||= do { warn "compilin +g tr$tr"; eval qq{sub {\$_[0]=~tr$tr}} }; } } $t = $s = "hello, world\n"; &{trd("/a-z/n-za-m/")}($s); print $s; &{tr +d("/a-z/A-Z/")}($t); print $t; &{trd("/a-z/n-za-m/")}($s); print $s;
Update: If you want to compile translation tables only once, I can ask, can't I compile a printf/scanf pattern, or a pack/unpack pattern once? I don't think creating a translation table would be so slow. One would have to benchmark it to say anything of course.
In reply to Re: efficient string translation?
by ambrus
in thread efficient string translation?
by 5mi11er
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |