Re: change [A-Z] to U, [a-z] to L with one regex?
by punch_card_don (Curate) on Mar 19, 2013 at 12:10 UTC
|
It may be useful to stop and ask why one would want to do that. Is there some compelling practical reason? Sometimes we just get caught up in the mindset that it's more elegant to condense as much as possible, but it's not always. The original code
$r =~ s/[A-Z]/U/g;
$r =~ s/[a-z]/L/g;
is so crystal clear and simple. It's probably pretty darn efficient too. Why garble it up just to save a line?
Time flies like an arrow. Fruit flies like a banana.
| [reply] [d/l] |
Re: change [A-Z] to U, [a-z] to L with one regex?
by BrowserUk (Patriarch) on Mar 19, 2013 at 11:02 UTC
|
$r =~ s[([a-zA-Z])][$1 lt '[' ? 'U' : 'L']ge;;
But this will be vastly more efficient: $r =~ tr[A-Z][U];
$r =~ tr[a-z][L];
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
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] [select] |
Re: change [A-Z] to U, [a-z] to L with one regex?
by hdb (Monsignor) on Mar 19, 2013 at 11:00 UTC
|
tr is the one that does it:
my $r = "SomEThingWithMIXEDRegiSter";
#$r =~ tr/[A-Z][a-z]/UUUUUUUUUUUUUUUUUUUUUUUUUULLLLLLLLLLLLLLLLLLLLLLL
+LLL/;
eval( '$r =~ tr/[A-Z][a-z]/'.('U' x 26).('L' x 26).'/' );
print $r;
The string in eval expands to the commented line above it.
For every complex problem there is an answer that is clear, simple, and wrong. H. L. Mencken
| [reply] [d/l] |
|
$r =~ tr/A-Za-z/UUUUUUUUUUUUUUUUUUUUUUUUUUL/;
| [reply] [d/l] [select] |
|
| [reply] |
|
|
| [reply] |
Re: change [A-Z] to U, [a-z] to L with one regex?
by daxim (Curate) on Mar 19, 2013 at 11:07 UTC
|
There are more than just upper-case and lower-case letter characters. Decide what you want to do with those you haven't considered yet, and put them into the map.
use Unicode::UCD qw(charinfo);
my %category_map = (
Lu => 'U', # letter, upper
Ll => 'L', # letter, lower
);
my $r = 'SomEThingWithMIXEDRegiSter';
$r =~ s
{
(.) # capture the character
}
{
$category_map{
charinfo(ord $1)->{category}
}
}egmsx;
# ULLUULLLLULLLUUUUUULLLULLL
| [reply] [d/l] |
|
s/(\p{Lu})|\p{Ll}/ defined($1) ? 'U' : 'L' /eg;
| [reply] [d/l] |
|
what about punctuation and numbers? =)
my 2¢
DB<141> $r = 'SomEThingWithMIXEDRegiSter;.?=356';
=> "SomEThingWithMIXEDRegiSter;.?=356"
DB<142> $r =~ s/(.)/ $1 eq "\u$1" ? ($1 eq "\l$1" ? $1 : "U" ) : ($1
+ eq "\l$1" ? "L" : "X" ) /ge
=> 33
DB<143> $r
=> "ULLUULLLLULLLUUUUUULLLULLL;.?=356"
Cheers Rolf
( addicted to the Perl Programming Language)
| [reply] [d/l] |
|
I'm pretty sure $1 eq "\u$1" can fail for some characters it shouldn't fail for.
| [reply] [d/l] |
Re: change [A-Z] to U, [a-z] to L with one regex?
by rjt (Curate) on Mar 19, 2013 at 23:05 UTC
|
$_ = "SomEThingWithMIXEDRegiSter\n";
y/A-Z/U/, y/a-z/L/;
print;
Yes, this is technically still two expressions, but as others have stated, it'll be efficient, and to me, anyway, the comma operator has good semantic meaning, here, even though a semicolon would do just as well.
Update: Corrected bracket typo in y///. Thanks jwkrahn. | [reply] [d/l] [select] |
|
y/A-Z/U/, y/a-z/L/;
| [reply] [d/l] [select] |