in reply to Variables in Regular Expressions?

There are several faults here:
~= should probably be =~
tr does a translation from one character to another and has nothing to do with regular expressions.
This should probably be s/...

Try:

$a = "abc"; $b = "def"; $c = "abc leppard"; $c =~ s/$a/$b/;

Replies are listed 'Best First'.
Re: Re: Variables in Regular Expressions?
by pchou (Novice) on Apr 25, 2001 at 23:43 UTC
    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
      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