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.
| [reply] [d/l] |
Re: Reversing string case
by Ratazong (Monsignor) on Aug 25, 2010 at 11:03 UTC
|
| [reply] |
|
|
The code i could think of
if ($string =~ /[A-Z]+/){
$string = lc $string;
}else{
$string = uc $string;
}
| [reply] [d/l] |
|
|
| [reply] |
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.
| [reply] [d/l] [select] |
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
| [reply] [d/l] |
Re: Reversing string case
by Khen1950fx (Canon) on Aug 25, 2010 at 12:42 UTC
|
/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;
| [reply] [d/l] |
|
|
| [reply] |
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? | [reply] |
|
|
| [reply] |
|
|
my $reversed = $str =~ /\p{Lu}/ ? lc $str : uc $str; # Or
my ($reversed) = map {/\p{Lu}/ ? lc : uc} $str;
| [reply] [d/l] |
Re: Reversing string case
by nvivek (Vicar) on Aug 25, 2010 at 11:36 UTC
|
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";
}
| [reply] [d/l] |
|
|
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.
| [reply] [d/l] [select] |