Using a variable within a tr//

Discussion in 'Perl Misc' started by David Filmer, Jul 6, 2003.

  1. David Filmer

    David Filmer Guest

    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
     
    David Filmer, Jul 6, 2003
    #1
    1. Advertising

  2. David Filmer

    Sam Holden Guest

    On Sun, 06 Jul 2003 22:33:16 GMT,
    David Filmer <> wrote:
    > 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? :)

    --
    Sam Holden
     
    Sam Holden, Jul 6, 2003
    #2
    1. Advertising

  3. David Filmer

    bd Guest

    On Sun, 06 Jul 2003 23:46:53 +0000, David Filmer wrote:

    >> 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?

    >
    > 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.

    --
    Freenet distribution not available
    Your fly might be open (but don't check it just now).
     
    bd, Jul 7, 2003
    #3
  4. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    "David Filmer" <> wrote in
    news:Ms1Oa.21787$I8.13027@rwcrnsc53:

    > 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-----
     
    Eric J. Roode, Jul 7, 2003
    #4
  5. David Filmer <> wrote:

    > 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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jul 7, 2003
    #5
  6. David Filmer

    Sam Holden Guest

    On Sun, 6 Jul 2003 19:17:24 -0500, Tad McClellan <> wrote:
    > David Filmer <> wrote:
    >
    >> 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.


    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.

    --
    Sam Holden
     
    Sam Holden, Jul 7, 2003
    #6
  7. David Filmer

    David Filmer Guest

    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'

    "Tad McClellan" <> wrote in message
    news:...
    > David Filmer <> wrote:
    >
    > > 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.
    >
    >
    > --
    > Tad McClellan SGML consulting
    > Perl programming
    > Fort Worth, Texas
     
    David Filmer, Jul 7, 2003
    #7
  8. "David Filmer" <> wrote in message
    news:Ms1Oa.21787$I8.13027@rwcrnsc53...
    > 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 ...".
     
    William Alexander Segraves, Jul 7, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Friday
    Replies:
    4
    Views:
    465
    Friday
    May 17, 2005
  2. mfglinux
    Replies:
    11
    Views:
    705
    Roberto Bonvallet
    Sep 12, 2007
  3. Abe
    Replies:
    2
    Views:
    169
  4. Krishna Komoravolu

    newbie ?: expand variable within a variable

    Krishna Komoravolu, Oct 31, 2003, in forum: Perl Misc
    Replies:
    4
    Views:
    124
    Gunnar Hjalmarsson
    Oct 31, 2003
  5. David Filmer
    Replies:
    19
    Views:
    246
    Kevin Collins
    May 21, 2004
Loading...

Share This Page