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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Reversing string case
by BrowserUk (Patriarch) on Aug 25, 2010 at 13:39 UTC

    If you live in a non-unicode world, then tr///was designed for this:

    $s = 'This is A mIxEd-Case STRING';; $s =~ tr[A-Za-z][a-zA-Z];; print $s;; tHIS IS a MiXeD-cASE string

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Reversing string case
by Ratazong (Monsignor) on Aug 25, 2010 at 11:03 UTC

    What did you try so far? At which point did you fail? Have you looked at the functions uc and lc?

    HTH, Rata
      The code i could think of
      if ($string =~ /[A-Z]+/){ $string = lc $string; }else{ $string = uc $string; }
        Your code lower-cases the whole string if a singe uppercase character is found. Is that what you want? What should happen to strings with mixed case?

        Also you ignore all non-ASCII upper case letters.

        Perl 6 - links to (nearly) everything that is Perl 6.
Re: Reversing string case
by moritz (Cardinal) on Aug 25, 2010 at 11:36 UTC
    How do is the case of a string identified?

    For a letter you can use the regex /\p{Lu}/ which matches upper case letters, \p{Ll} for lower case, and \p{Lt} for title case. Also remember that many characters don't have the lower case/upper case distinction (like digits, and letters from many different writing systems).

    A crude way would also be a test like lc($x) eq $x

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: Reversing string case
by jwkrahn (Abbot) on Aug 25, 2010 at 13:27 UTC
    $ perl -le'$_ = "MiXeD CaSe ExAmPlE"; print; s/([[:alpha:]])/ $1 eq "\ +U$1" ? "\L$1" : "\U$1" /eg; print' MiXeD CaSe ExAmPlE mIxEd cAsE eXaMpLe
Re: Reversing string case
by Khen1950fx (Canon) on Aug 25, 2010 at 12:42 UTC
    For all upper case or all lower case, just use Text::Autoformat.
    /usr/bin/perl use strict; use warnings; use Text::Autoformat; my $formatted = autoformat 'WHAT DO YOU WANT RETURNED', { case => 'lower' }; print $formatted; my $formatted1 = autoformat 'what do you want returned', { case => 'upper' }; print $formatted1;

      Text::Autoformat looks pretty cool but it seems like overkill compared to uc and lc.

Re: Reversing string case
by JavaFan (Canon) on Aug 25, 2010 at 11:07 UTC
    If your string is "FoO bAr", what should be returned?

      The use cases in my case are that the string will either be entirely in upper case of lowercase.

      Apologies for not mentioning this earlier.

        In that case:
        my $reversed = $str =~ /\p{Lu}/ ? lc $str : uc $str; # Or my ($reversed) = map {/\p{Lu}/ ? lc : uc} $str;
Re: Reversing string case
by nvivek (Vicar) on Aug 25, 2010 at 11:36 UTC

    Following code will help you.

    use strict; use warnings; print "Enter the string:"; my $string=<STDIN>; #getting the input from user chomp($string); #remove the newline at end in string if($string=~/^[a-z]+$/) { #checking whether the string is in lowercase print "String given in Lower Case:$string\n"; $string=~s/([a-z])/\u$1/g; #replacing the string to be in upper case print "Uppercase String:$string\n"; } elsif($string=~/^[A-Z]+$/) { #checking whether the string is in upperc +ase print "String given in Upper Case:$string\n"; $string=~s/([A-Z])/\l$1/g; #replacing the string to be in lower case print "Lowercase String:$string\n"; } elsif($string=~/^[a-zA-Z]+$/) { #checking whether the string is in mix +ed case print "String given in Mixed Case:$string\n"; $string=~s/([a-z])/\u$1/g; #replacing the string to be in upper case print "Uppercase String:$string\n"; $string=~s/([A-Z])/\l$1/g; #replacing the string to be in lower case print "Lowercase String:$string\n"; }

      Your mixed case test will not work the way you intended

      /[a-zA-Z]/ matches any lower- or uppercase character, not a lower- and then an uppercase one.

      And your two substitutions will not flip the case of a character because after the first substitution (i.e. s/([a-z])/\u$1/g) all characters are uppercase and will be changed to lowercase at the second substitution.