blinky has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to translate a string containing special characters such as %, @, $, etc. When I try to translate (, ) and # it doesn't work. Any ideas? I tried escaping them, but it didn't work either.

$_ = 'MA()A))Y#4';
tr/MA()Y#4/1234567/;
print $_;

Instead of printing 1234244567 I get 12()2))5#7.

Replies are listed 'Best First'.
Re: Transilteration problem
by Albannach (Monsignor) on Dec 07, 2000 at 01:38 UTC
    I wonder what OS and version of Perl you are using. What you wrote works exactly as expected under NT with ActiveState v5.6.0

    --
    I'd like to be able to assign to an luser

Re: Transilteration problem
by blinky (Initiate) on Dec 07, 2000 at 02:44 UTC
    Duh, I'm an idiot. It was working all along.
(Ovid) Re: Transilteration problem
by Ovid (Cardinal) on Dec 07, 2000 at 01:42 UTC
    Here's my test on WinNT, Perl v5.6:
    D:\perl\work>perl -e "$_='MA()A))Y#4';tr/MA()Y#4/1234567/;print" 1234244567

    Cheers,
    Ovid

    Update: Since other monks appear to not have a problem. Could you post a full code snippet so we can see what you're doing? Perhaps the tr/// is occurring inside of a loop where $_ is dynamically scoped?

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Transilteration problem
by AgentM (Curate) on Dec 07, 2000 at 01:42 UTC
Re: Transilteration problem
by decnartne (Beadle) on Dec 07, 2000 at 01:52 UTC
    works ok for:

    perl 5.005_03 on Solaris 8

    decnartne ~ entranced