If you want to use the same transliteration more than once, you could try evalling it to a sub like this:

my $tr = "/a-z/n-za-m/"; my $trs = eval qq{sub {\$_[0]=~tr$tr}}; $s = "uryyb, jbeyq\n"; &$trs($s); print $s;
This way, you can use the same transliteration any number of times and compile it only once.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.