Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

change [A-Z] to U, [a-z] to L with one regex?

by Doctrin (Beadle)
on Mar 19, 2013 at 10:47 UTC ( #1024254=perlquestion: print w/ replies, xml ) Need Help??
Doctrin has asked for the wisdom of the Perl Monks concerning the following question:

Hello dear Monks :) Say, I have following code:
my $r = "SomEThingWithMIXEDRegiSter"; $r =~ s/[A-Z]/U/g; $r =~ s/[a-z]/L/g; print $r;
Is there a way to make changes made by second and third strings with one regex? I mean - change all uppercases to "U" AND all lowercases to "L" at once? Thanks

Comment on change [A-Z] to U, [a-z] to L with one regex?
Download Code
Re: change [A-Z] to U, [a-z] to L with one regex?
by hdb (Parson) 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
      #$r =~ tr/[A-Z][a-z]/UUUUUUUUUUUUUUUUUUUUUUUUUULLLLLLLLLLLLLLLLLLLLLLL +LLL/; eval( '$r =~ tr/[A-Z][a-z]/'.('U' x 26).('L' x 26).'/' );

      [A-Z] is 28 characters, not 26, so your expression is wrong.

      This will work:

      $r =~ tr/A-Za-z/UUUUUUUUUUUUUUUUUUUUUUUUUUL/;
        A-Z is 28 characters, not 26,

        Since when? (Did unicrap fuck that up n'all?)


        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.
        Thanks for your comment. I did not realize that in tr you would not need [] and that they would be treated as characters in their own right. And indeed in my script a "Z" would be translated into "L". W/o the brackets it would work. I should read the manuals more often/carefully.
        For every complex problem there is an answer that is clear, simple, and wrong. H. L. Mencken
Re: change [A-Z] to U, [a-z] to L with one regex?
by BrowserUk (Pope) 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.
Re: change [A-Z] to U, [a-z] to L with one regex?
by daxim (Chaplain) 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
      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)

        I'm pretty sure $1 eq "\u$1" can fail for some characters it shouldn't fail for.

      Simpler solution not limited to non-ASCII chars:

      s/(\p{Lu})|\p{Ll}/ defined($1) ? 'U' : 'L' /eg;
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.
Re: change [A-Z] to U, [a-z] to L with one regex?
by rjt (Deacon) on Mar 19, 2013 at 23:05 UTC

    You can always use $_:

    $_ = "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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1024254]
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (11)
As of 2014-09-01 08:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (299 votes), past polls