Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

uppercase/lowercase in a single expression

by greatshots (Pilgrim)
on Sep 07, 2006 at 06:10 UTC ( #571612=perlquestion: print w/ replies, xml ) Need Help??
greatshots has asked for the wisdom of the Perl Monks concerning the following question:

monks,

I would like to convert the contents of a file to uppercase if a character is lowercase and vice-versa.
How can I do that ?

Comment on uppercase/lowercase in a single expression
Re: uppercase/lowercase in a single expression
by McDarren (Abbot) on Sep 07, 2006 at 06:26 UTC
    You need the uc (upercase) and lc (lowercase) functions, and a conditional statement such as "if".

    This smells a bit like homework, so I'll leave you to work the rest out for yourself.

    Update: actually, you're probably better off with the transliteration operator (tr). This one didn't occur to me straight off as I rarely use it myself. See perlop for details. Also, if you want to edit the file without opening it, then you'll be interested in the 'i' and 'p' command line switches - see perlrun for details on these.

      McDarren

      $line1 = 'asdadAADSAsdfsfASASDjljsdASDAS'; $line2 = 'GKHSKJADHasdadhkadhGHKJHKJHasdada';
      do you mean to say that should I need traverse each character and change it to upper/lower ?

        Well, that's exactly what tr does.

        Example:

        perl -le '$line1 = "asdadAADSAsdfsfASASDjljsdASDAS"; $line1 =~ tr/[A-Z +a-z]/[a-zA-Z]/;print $line1;'
        Prints:
        ASDADaadsaSDFSFasasdJLJSDasdas
        thanks a lot.
Re: uppercase/lowercase in a single expression
by mickeyn (Priest) on Sep 07, 2006 at 07:23 UTC
    You can also try this:
    use Tie::File; tie @array, 'Tie::File', "./text_file"; foreach (@array){ tr/[a-zA-Z]/[A-Za-z]/; } untie @array;
    Enjoy,
    Mickey
      .. but I really wouldn't.
Re: uppercase/lowercase in a single expression
by johngg (Abbot) on Sep 07, 2006 at 08:38 UTC
    You could edit the file in place from the command line like this

    perl -pi.bak -e 'tr{A-Za-z}{a-zA-Z};' filename

    You will need to use double-quotes if on MS Windows.

    Cheers,

    JohnGG

Re: uppercase/lowercase in a single expression (locale)
by tye (Cardinal) on Sep 07, 2006 at 14:16 UTC

    Ah, but nobody's reply has solved the problem of swapping the case of letters while obeying locale (so that accented letters are handled).

    s/([[:upper:]]*)([[:lower:]]*)/\L$1\U$2/g;

    - tye        

Re: uppercase/lowercase in a single expression
by chargrill (Parson) on Sep 07, 2006 at 17:07 UTC

    In the spirit of TIMTOWTDI, here's a pointless regex version. But if this indeed IS homework, turning this in will certainly impress your teacher.

    #!/usr/bin/perl use strict; use warnings; my $line1 = 'asdadAADSAsdfsfASASDjljsdASDAS'; my $line2 = 'GKHSKJADHasdadhkadhGHKJHKJHasdada'; for my $word( $line1, $line2 ){ print "Before: $word\n After: "; $word =~ /((\w)\W*(?{ ord $2 > 96 })(?{ print $^R ? uc $2 : lc $2 }) +)+/; print "\n\n"; }

    Output:

    Before: asdadAADSAsdfsfASASDjljsdASDAS After: ASDADaadsaSDFSFasasdJLJSDasdas Before: GKHSKJADHasdadhkadhGHKJHKJHasdada After: gkhskjadhASDADHKADHghkjhkjhASDADA


    --chargrill
    $,=42;for(34,0,-3,9,-11,11,-17,7,-5){$*.=pack'c'=>$,+=$_}for(reverse s +plit//=>$* ){$%++?$ %%2?push@C,$_,$":push@c,$_,$":(push@C,$_,$")&&push@c,$"}$C[$# +C]=$/;($#C >$#c)?($ c=\@C)&&($ C=\@c):($ c=\@c)&&($C=\@C);$%=$|;for(@$c){print$_^ +$$C[$%++]}

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://571612]
Approved by McDarren
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (13)
As of 2014-11-26 15:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (171 votes), past polls