in reply to Re: Variables in Regular Expressions?
in thread Variables in Regular Expressions?

I was trying to come up with an interesting example but failed. I actually _do_ want to do translation. (I'm writing a little crytpogram helper tool.) I want to replace all the letters from an alphabet, say $a, with all the letters from the new alphabet, say $b.
# the alphabet $a = "abcdefghijklmnopqurtuvwxyz"; # i've discovered that a represents T in the cryptogram, for example $b = "T E "; $c = "the text i want to decipher"; $c =~ tr/$a/$b/;
So basically, is there any way to get this to work? Thanks for your help.
-Paul

Replies are listed 'Best First'.
Re: Re: Re: Variables in Regular Expressions?
by busunsl (Vicar) on Apr 25, 2001 at 23:55 UTC
    Ok, here it is:

    Right from the perlop manpage:

    Because the transliteration table is built at compile time, neither the SEARCHLIST nor the REPLACEMENTLIST are subjected to double quote interpolation. That means that if you want to use variables, you must use an eval(): eval "tr/$oldlist/$newlist/"; die $@ if $@; eval "tr/$oldlist/$newlist/, 1" or die $@;
    Do a perldoc perlop to read all about tr