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

I have file that has multiple mixed cased words. I was woundering if there is anyway to do a mass tr/// on any word no matter what the letters are.
Example:
PROjECT_RIgHTS, APPLICATION_RIgHTs
How could I change each set without doing
$_ =~ tr/PROjECT_RIgHTS/PROJECT_RIGHTS/; $_ =~ tr/APPLICATION_RIgHTs/APPLICATION_RIGHTS/;<br>
Or, is this the only way.
Thanks
Bobby

Replies are listed 'Best First'.
Re: tr/<mixed string>/<UPPER STRING>/
by Helter (Chaplain) on Sep 26, 2002 at 14:20 UTC
    Or of course there is:

    uc();
    or.
    "\U$_"
      Yes, and in fact uc is a more robust solution than tr/a-z/A-Z/ because it respects the current locale and Unicode uppercase mappings. ++Helter

      -- Mike

      --
      just,my${.02}

        I'll second that thumbs-down for using tr. The instant you throw in accented words, you're going to cause mayhem. "RESUMé" being a simple example.

        How about this?
        % perl -pi -e '$_=uc' file1 file2 ...
        Careful where you aim it, because it modifies the files in place. Remember to make backups first if you're wary.

        In order to make \U work with accented chars I had to start my program like this:

        $loc = 'pt_PT.ISO_8859-1'; # my locale $ENV{'LANG'} = $loc; $ENV{'LC_ALL'} = $loc; $ENV{'LOCPATH'} = '/usr/share/locale'; use locale; require POSIX; POSIX::setlocale(&POSIX::LC_ALL, $loc);

        Is there a shorter way?

Re: tr/<mixed string>/<UPPER STRING>/
by rob_au (Abbot) on Sep 26, 2002 at 14:17 UTC
    How about simply ...

    tr/a-z/A-Z/

    The transliteration operator (tr) is designed to work with lists of characters, replacing the character in the search list with the corresponding character in the replacement list. Additionally a character range can be specified with a hyphen, such that tr/A-E/0-5/ is equivalent to tr/ABCDE/01234/ - See perlop for details.

     

      Thanks, that worked!
      I knew that I should use tr///, but the last time I used
      tr/a-z/A-Z/ it didn't work the way it was suppost to. This time it did.
      Thanks again
      Bobby
      Ah, the beauty of perl.
Re: tr/<mixed string>/<UPPER STRING>/
by zigdon (Deacon) on Sep 26, 2002 at 14:24 UTC
    you don't have to use tr for this - just use the built in uc function:
    $_ = uc;
    or, if you're using the var in a string, you can use the \U:
    $_ = "\U$_";

    -- Dan

Re: tr/<mixed string>/<UPPER STRING>/
by virtualsue (Vicar) on Sep 26, 2002 at 14:32 UTC
    Here is a filter that will do the job from the command line:
    perl -pi.bak -e '$_ = uc' your_files
Re: tr/<mixed string>/<UPPER STRING>/
by Preceptor (Deacon) on Sep 26, 2002 at 14:18 UTC
    cat $file | tr [:upper:] [:lower:] | sed 's/<pattern1>/<pattern2>/ Although I suspect what you want to be doing is something like,
    $_ =~ s/PROJECT_RIGHTS/PROJECT_RIGHTS/gi
    (the 'i' says ignore case)
    --
    It's not pessimism if there is a worse option, it's not paranoia when they are and it's not cynicism when you're right.