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

What is the best method in PERL for converting a variable value from lower case to uppercase ? ie: convert $i - w2k to $i - W2K thanks

Replies are listed 'Best First'.
Re: Case conversion
by spurperl (Priest) on Mar 31, 2003 at 07:44 UTC
    Check the uc function.
    perldoc -f uc
    For example:
    perl -e 'print "hello " . uc "world"'
    Prints: hello WORLD...
Re: Case conversion
by Heidegger (Hermit) on Mar 31, 2003 at 07:45 UTC
Re: Case conversion
by hotshot (Prior) on Mar 31, 2003 at 07:46 UTC
    you can use function uc for that.
    another option is:
    $var =~ tr/a-z/A-Z/;
    but the first way is better.

    Hotshot
Re: Case conversion
by perlguy (Deacon) on Mar 31, 2003 at 15:57 UTC
    You can also use \U and \u in a double quoted string. Such as:
    my $i = 'hellO, worLd'; print "\U$i\n"; # All uppercase print "\L\u$i\n"; # All lowercase, except the 1st letter
Re: Case conversion
by Heidegger (Hermit) on Mar 31, 2003 at 07:49 UTC
    The transliterate regexp (tr) once was very useful to me when I had to upper case Lithuanian characters in a web app. I could't make the Lithuanian perl locale work. So the transliterate was the only option.
Re: Case conversion
by collywobble (Initiate) on Mar 31, 2003 at 07:57 UTC
    Thanks for the help. uc / lc was all I needed
      And I really started to think that he needs case conversion in the name of the variable.
Re: Case conversion
by collywobble (Initiate) on Mar 31, 2003 at 07:48 UTC
    I should probably phase this a bit better. I have a varible and this can be in either lower or upper case. For me to use this variable it needs to be in upper case. What I would like to achieve is to convert the variable to upper case if it is currently lower case otherwise just use it as it is. I hope that's better
      The simplest (and thus probably the desired) solution is to use uc anyway, it won't modify upper-case strings and will only turn lower-case to upper-case.

      Try:
      perl -e 'print "hello " . uc "WOrld"'