Is there a way to use variables in a translation?  I'm looking for a way
to use, for example, $x = "abc" and $y = "ABC" so something like

y/$x/$y/;

behave like

y/[abc]/[ABC]/;

The tr/// operator doesn't interpolate so you have to do something like:

eval y/$x/$y/;

I tried using that, but $test wasn't changed here:

        $test = "This is a test.";

        $x = "is";
        $y = "si";

        eval $test =~ y/$x/$y/;

        print "$test\n";

I'm not sure of the syntax for eval in the case you recommended, but I tried some things (adding braces, moving eval next to "y"...the kind of stuff someone who doesn't really know what he's doing is likely to try).

After seeing Uri's email, what should be quoted on the eval line?

Or perhaps:

my %translate;
@translate{ split //, $x } = split //, $y;
s/([$x])/ $translate{ $1 } /eg;

Or if you just want to upper case certain letters then:

s/([$x])/\U$1/g;

I have a loop method that already does what I need, but I'm hoping to find a way to do it with y/. I thought it might be easier, but maybe not.

Just curious, in y/[abc]/[ABC]/; why are you translating all '[' characters to '[' and all ']' characters to ']'?

My turn for "oops". Beginner's typo. I was thinking of regular expressions.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to