Using a variable within a tr//

D

David Filmer

I can't seem to do:

$vowels = 'aeiou'; $VOWELS = 'AEIOU';
tr/$vowels/$VOWELS/; #make all vowels uppercase

Perl doesn't see '$vowels' as a variablename, but as a 7-byte string
literal.

Of course, it works fine if I say

tr/aeiou/AEIOU/;

but I would like to know the proper syntax to do it with variablenames.....

TIA
 
S

Sam Holden

I can't seem to do:

$vowels = 'aeiou'; $VOWELS = 'AEIOU';
tr/$vowels/$VOWELS/; #make all vowels uppercase

Perl doesn't see '$vowels' as a variablename, but as a 7-byte string
literal.

Of course, it works fine if I say

tr/aeiou/AEIOU/;

but I would like to know the proper syntax to do it with variablenames.....

Which part of the documentation (perldoc perlop - the section on tr//) that
describes this do you find hard to understand. It has a code example
that does exactly what you want; how do you think that can be fixed so
that you can understand it?

Or did you not bother to read the documentation, and instead practice
"programming by guess"?

Is it obvious I haven't slept for far too long? :)
 
B

bd

Thanks for your response; this led me down the path of enlightnment.

The part of the documentation that I didn't understand was WHERE the blasted
'tr' operation was documented. I didn't know it was in 'perlop.' I tried
'perldoc tr' and got nothing. Since you were kind enough to ask, the docs
could be fixed (such that even MY limited understanding could grasp it) by
linking each and every reserved Perl expression such that if I type 'perldoc
tr' then I actually GET the 'tr' docs (whether they're in 'perlop' or
'foobar' or whereever). Or prehaps there's already a method to do this that
I'm unaware of...

Did you try 'perldoc -f tr'? It gives me:
tr/// The transliteration operator. Same as "y///". See perlop.

perldoc (non-option) is mostly for the perl* pages and modules. perldoc -f
is for built-in functions and operators.
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I can't seem to do:

$vowels = 'aeiou'; $VOWELS = 'AEIOU';
tr/$vowels/$VOWELS/; #make all vowels uppercase

Perl doesn't see '$vowels' as a variablename, but as a 7-byte string
literal.

Arguably, this is a bug. Well, not really a bug, but a design problem that
probably "should" be fixed someday.

I see no rational reason why one should not be permitted to do the tr///
the way it occurred to you do to it. It's a very natural thing to want to
do.

Unfortunately, the only way to do it in current versions of perl is to wrap
the darn thing in an eval:

eval "tr/$vowels/$VOWELS/";

which strikes me as sort of lame, but hey it works.

- --
Eric
$_ = reverse sort qw p ekca lre Js reh ts
p, $/.r, map $_.$", qw e p h tona e; print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA+AwUBPwi7w2PeouIeTNHoEQJz1gCguIQuqObLU2vvPN8Os8dwskxbl9IAmL5q
dD5JVXQuX4xSyGumIZMLWrg=
=0wm2
-----END PGP SIGNATURE-----
 
T

Tad McClellan

David Filmer said:
The part of the documentation that I didn't understand was WHERE the blasted
'tr' operation was documented. I didn't know it was in 'perlop.' I tried
^^^^^^^^^


You know it is an "operation" but you didn't think to look where
the operators are documented?

'perldoc tr' and got nothing.

perldoc -f tr

would have told you where the docs are.
 
S

Sam Holden

^^^^^^^^^


You know it is an "operation" but you didn't think to look where
the operators are documented?



perldoc -f tr

would have told you where the docs are.

That's how I found them :)

I actually expected the posted code to work, and was surprised when it
didn't. It took me all of 20 seconds to find the answer in the docs
(and most of that was forgetting to use '^' when searching for
the appropriate section in perlop...

Just shows that you can program in perl for years and not even know
the most basic of things... Or maybe it shows I'm amazingly dim.
 
D

David Filmer

I did not know that there was even such a section as perlop. Most
'operations' are pretty self-evident and don't require documentation to use,
at least for basic tasks. I've never before needed docs for an operation.

Thanks to those who educated me about the '-f' flag. I was unaware of this
functionality, as I've always been successful at finding docs on whatever
I'm looking for with the simple 'perldoc whatever' command (because I happen
to have sought info only on standalone topics - just lucky, I guess). I was
quite surprised when I found nothing under 'perldoc tr'
 
W

William Alexander Segraves

David Filmer said:
I can't seem to do:

$vowels = 'aeiou'; $VOWELS = 'AEIOU';
tr/$vowels/$VOWELS/; #make all vowels uppercase

Perl doesn't see '$vowels' as a variablename, but as a 7-byte string
literal.

Of course, it works fine if I say

tr/aeiou/AEIOU/;

but I would like to know the proper syntax to do it with variablenames.....

TIA

See pp. 74-76, PP2e (Camel Book).

eval "tr/$vowels/$VOWELS/, 1" or die $@; # make all vowels uppercase

works fine here, as suggested in PP2e.

Cheers.

Bill Segraves

P.S. Hint: tr/// is an *op*erator. Look for documentation in perlop. Even if
you called it it a function instead and looked in perlfunc, you'd be
referred to perlop for details. Look in perlop at the end of "Regexp
Quote-Like Operators", just before "Gory Details ...".
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top