Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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:


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

Replies are listed 'Best First'.
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.


      $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.


        perl -le '$line1 = "asdadAADSAsdfsfASASDjljsdASDAS"; $line1 =~ tr/[A-Z +a-z]/[a-zA-Z]/;print $line1;'
        thanks a lot.
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.



Re: uppercase/lowercase in a single expression (locale)
by tye (Sage) 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).


    - tye        

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;
      .. but I really wouldn't.
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"; }


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

    $,=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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://571612]
Approved by McDarren
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2018-06-18 00:46 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (107 votes). Check out past polls.