Need help wih tr/// operator

M

Master Web Surfer

[This followup was posted to comp.lang.perl.misc]

I need help with solving a problem with the tr/// operator


$v1 = "aeiou";
$v2 = "AEIOU";
$string1 = "Hello out there";
$string1 =~ tr/${v1}/${v2}/;
print "$string1\n";

The output I see is

Hello out there

Why weren't *all* the vowels translated?

I am running perl 5.8.0 under Windows XP.

I have even tried using an eval statement, but with no luck.
 
M

Matthew Braid

Master said:
[This followup was posted to comp.lang.perl.misc]

I need help with solving a problem with the tr/// operator


$v1 = "aeiou";
$v2 = "AEIOU";
$string1 = "Hello out there";
$string1 =~ tr/${v1}/${v2}/;
print "$string1\n";

The output I see is

Hello out there

Why weren't *all* the vowels translated?

I am running perl 5.8.0 under Windows XP.

I have even tried using an eval statement, but with no luck.

Unfortunately tr/// does not do interpolation of variables. What you're
asking tr/// to do here is replace '$' with '$', '{' with '{', 'v' with
'v', '1' with '2', and '}' with '}'.

ie, if you change your string to "Hello out there 123" you'll end up
with "Hello out there 223". According to the docs (perldoc perlop, do a
search for 'tr/') the only way to do it is wrap it in a stringy eval,
which is nasty for a whole bunch of other reasons.

MB
 
J

John W. Krahn

Master said:
I need help with solving a problem with the tr/// operator

$v1 = "aeiou";
$v2 = "AEIOU";
$string1 = "Hello out there";
$string1 =~ tr/${v1}/${v2}/;
print "$string1\n";

The output I see is

Hello out there

Why weren't *all* the vowels translated?

Unlike m// and s///, tr/// doesn't interpolate so you are translating
'$' to '$' and '{' to '{' and 'v' to 'v' and '1' to '2' and '}' to '}'.
One way to do what you want is:

$string1 =~ s/[$v1]/\u$1/g;


John
 
A

A. Sinan Unur

[This followup was posted to comp.lang.perl.misc]

I need help with solving a problem with the tr/// operator


$v1 = "aeiou";
$v2 = "AEIOU";
$string1 = "Hello out there";
$string1 =~ tr/${v1}/${v2}/;
print "$string1\n";

The output I see is

Hello out there

Why weren't *all* the vowels translated?

I am running perl 5.8.0 under Windows XP.

I have even tried using an eval statement, but with no luck.

This indicates that you have read perldoc perlop, especially:

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():

Here is what works for me

C:\Home>cat t.pl
#! perl

use strict;
use warnings;

my $v1 = 'aeiou';
my $v2 = 'AEIOU';
my $s = 'Hello out there';

for ($s) {
eval qq(tr /$v1/$v2/);
die $@ if $@;
}

print $s, "\n";

C:\Home>t.pl
HEllO OUt thErE

I am not sure this is the best 'solution' as I am not that well versed in
Perl.

Sinan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top