in reply to The indisputable speed of tr///

The line
our $data = <DATA>;
is still useless because you read to $data only the first string from __DATA__ section :)
Please modify it to
our $data = join "", <DATA>;
IMHO it is exactly what you wanted to do:)

The worst thing about tr/// is that it can produce only character-to-character mapping. The best solution aviding this problem I found uses eval at compilation time:

BEGIN { our %rot = ( 'A' => 'N','B' => 'O','C' => 'P','D' => 'Q','E' => 'R','F' => 'S', 'G' => 'T','H' => 'U','I' => 'V','J' => 'W','K' => 'X','L' => 'Y', 'M' => 'Z','N' => 'A','O' => 'B','P' => 'C','Q' => 'D','R' => 'E', 'S' => 'F','T' => 'G','U' => 'H','V' => 'I','W' => 'J','X' => 'K', 'Y' => 'L','Z' => 'M','a' => 'n','b' => 'o','c' => 'p','d' => 'q', 'e' => 'r','f' => 's','g' => 't','h' => 'u','i' => 'v','j' => 'w', 'k' => 'x','l' => 'y','m' => 'z','n' => 'a','o' => 'b','p' => 'c', 'q' => 'd','r' => 'e','s' => 'f','t' => 'g','u' => 'h','v' => 'i', 'w' => 'j','x' => 'k','y' => 'l','z' => 'm', ); my $substs = join "\n", map { " s/$_/$rot{$_}/g;" } keys %rot; eval <<SUB; sub rot13s { local \$_ = shift; $substs; return \$_; } SUB }
It is only 52 times slower than tr/// :
Rate table s///g tr table 448/s -- -78% -100% s///g 2026/s 352% -- -98% tr 106383/s 23654% 5152% --

Replies are listed 'Best First'.
Re^2: The indisputable speed of tr///
by ikegami (Patriarch) on Jun 28, 2006 at 17:09 UTC

    The problem about only reading the first line of the string was raised and fixed in the first post of this thread.

    Your solution doesn't work. For example,
    print(rot13s(rot13s('AN')), "\n");
    prints 'AA' or 'NN' (depending on the hash ordering), but not the expected 'AN'.

    You also don't escape the keys and value of %rot as you should. The OP said this is a simplification of his problem, so the presense of punctuation in %rot is likely.

    Refer to an earlier post of mine for a working solution using eval to build a substitution and a working solution that uses substitution without using eval.

    "[rot13s] is only 52 times slower than [rot13tr]" makes no sense. I think you meant "rot13tr is only 52 times faster than rot13s".

    *Only* 52 times faster? So if rot13tr takes 30 seconds — The OP said he had lots of data to process — rot13s takes *only* 26 minutes? I wish my 30 minutes drive home every night only took 30 seconds.

      yes, i was wrong :) i forgot that i at first substitute all A to N and then all N to A, INCLUDING those A's which were already mapped to N :))