Cory Petkovsek wrote:
>How can I use a variable as a search or replace selection using tr//?
>
>$_ = "be!happy";
>$before="!;
>$after=" ";
>tr/$before/$after/;
>print $_;
>
Pulled from the perlop man page:
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 $@;
so substitute
eval "tr/$before/$after/";
for
tr/$before/$after/;
Hope it helps,
Kahli