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. | [reply] |
$line1 = 'asdadAADSAsdfsfASASDjljsdASDAS';
$line2 = 'GKHSKJADHasdadhkadhGHKJHKJHasdada';
do you mean to say that should I need traverse each character and change it to upper/lower ?
| [reply] [d/l] |
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
| [reply] [d/l] [select] |
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 | [reply] [d/l] |
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;
| [reply] [d/l] |
use Tie::File;
tie @array, 'Tie::File', "./text_file";
foreach (@array){
tr/[a-zA-Z]/[A-Za-z]/;
}
untie @array;
Enjoy,
Mickey | [reply] [d/l] |
.. but I really wouldn't.
| [reply] |
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[$%++]}
| [reply] [d/l] [select] |