cASE sWITCHING?

O

Oldbitcollector

Ok,

One more strange question tonight... (I hope)

Can someone help me find a way to convert the following var correctly?

$var='tHIS iS A rATHER sTRANGE dATA sTREAM';

I need a way to convery it on the fly from:

tHIS iS A rATHER sTRANGE dATA sTREAM
to
This Is a Rather Strange Data Stream


If I use s/[A-Z]/[a-z]/ then all caps are dropped to lowercase.
Not the answer I'm fishing for. Can someone assist with a line which
will
simple switch LOWER and upper case?

Thank again:
Oldbitcollector
 
T

Tad McClellan

Oldbitcollector said:
$var='tHIS iS A rATHER sTRANGE dATA sTREAM';

I need a way to convery it on the fly from:

tHIS iS A rATHER sTRANGE dATA sTREAM
to
This Is a Rather Strange Data Stream


$var =~ s/(\S+)/\u\L$1/g;

Can someone assist with a line which
will
simple switch LOWER and upper case?


$var =~ tr/a-zA-Z/A-Za-z/;
 
J

John Bokma

Oldbitcollector said:
Ok,

One more strange question tonight... (I hope)

Can someone help me find a way to convert the following var correctly?

$var='tHIS iS A rATHER sTRANGE dATA sTREAM';

I need a way to convery it on the fly from:

tHIS iS A rATHER sTRANGE dATA sTREAM
to
This Is a Rather Strange Data Stream

Read about tr

perldoc perlop
 
M

MrReallyVeryNice

Oldbitcollector said:
Ok,

One more strange question tonight... (I hope)

Can someone help me find a way to convert the following var correctly?

$var='tHIS iS A rATHER sTRANGE dATA sTREAM';

I need a way to convery it on the fly from:

tHIS iS A rATHER sTRANGE dATA sTREAM
to
This Is a Rather Strange Data Stream


If I use s/[A-Z]/[a-z]/ then all caps are dropped to lowercase.
Not the answer I'm fishing for. Can someone assist with a line which
will
simple switch LOWER and upper case?

Thank again:
Oldbitcollector

Give a try to the following code:

use strict;
use warnings;

my @sentence = qw/tHIS iS A rATHER sTRANGE dATA sTREAM/;
#print $_ . " " for @sentence;
#print "\n";
foreach (@sentence) {
if (lc($_) ne "a" ) {
print ucfirst(lc($_)) . " ";
}
else {
print lc($_) . " ";
}
}
 
J

John Bokma

$var =~ s/(^\S+)|(\S+)/
if (length($1) > 0) {
"\u\L$1"
} elsif (length($2) == 1) {
"\l$2"
} else {
"\u\L$2"
}/eg;

The OP didn't specify what should happen with i/I.

Since it looks like: make all lowercase upper and vice versa, I would use
tr.
 
I

Ilya Zakharevich

[A complimentary Cc of this posting was sent to
Oldbitcollector
tHIS iS A rATHER sTRANGE dATA sTREAM
to
This Is a Rather Strange Data Stream

The tr/// solution works for ASCII. In general, I have no idea what
to do with Titlecase chars; if you do not have Titlecase chars, then


join '', map { lc eq $_ ? uc $_ : lc $_ } split //, $string

(untested) should work.

Hope this helps,
Ilya
 
R

robic0

Oldbitcollector said:
Ok,

One more strange question tonight... (I hope)

Can someone help me find a way to convert the following var correctly?

$var='tHIS iS A rATHER sTRANGE dATA sTREAM';

I need a way to convery it on the fly from:

tHIS iS A rATHER sTRANGE dATA sTREAM
to
This Is a Rather Strange Data Stream


If I use s/[A-Z]/[a-z]/ then all caps are dropped to lowercase.
Not the answer I'm fishing for. Can someone assist with a line which
will
simple switch LOWER and upper case?

Thank again:
Oldbitcollector

Use tr/// if all you want to do is switch u<->l.
But if you want to controll the beginning of the sentence
or certain single stand alone chars, this might do it:

use strict;
use warnings;

my %SingleCaps = ('a' => '');
my $var = "a real funky one: THIS iS A rATHER sTRANGE dATA sTREAM I
will not forget";

$var =~ s/(^\S+)|(\S+)/
if (defined $1) {"\u\L$1"} # Always cap first word of sentence
?
elsif (length($2) == 1 && exists $SingleCaps{lc($2)}) {
"\l$2"
} else {
"\u\L$2"
}/eg;


print $var,"\n";

__END__

output:
A Real Funky One: This Is a Rather Strange Data Stream I Will Not
Forget
 
E

Eric J. Roode

Oldbitcollector wrote: [...]
If I use s/[A-Z]/[a-z]/ then all caps are dropped to lowercase.
Not the answer I'm fishing for. Can someone assist with a line which
will
simple switch LOWER and upper case?

Thank again:
Oldbitcollector

Give a try to the following code:

use strict;
use warnings;

my @sentence = qw/tHIS iS A rATHER sTRANGE dATA sTREAM/;
#print $_ . " " for @sentence;
#print "\n";
foreach (@sentence) {
if (lc($_) ne "a" ) {
print ucfirst(lc($_)) . " ";
}
else {
print lc($_) . " ";
}
}

What the **** is this ucfirst/lc crap??

Did you even READ what the OP wanted? Or do you claim to be a psychic, who
can magically read his mind and divine that he wants to capitalize words
(except for "a", for some reason)?

The OP wanted, and I quote here -- pay attention now -- a "simple switch
LOWER and upper case". Not some sentence or title capitalization.

To Oldbitcollector:

You need to learn more about tr. You wrote "s/[A-Z]/[a-z]/" but I suspect
you meant "tr/A-Z/a-z/"; probably you saw that somewhere and misremembered.
tr is not simply for changing things to lowercase. Try:

tr/A-Za-z/a-zA-Z/;

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
E

Eric J. Roode

Oldbitcollector:


Text::Capitalize

That does not do what the OP claimed to want to do.

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
R

robic0

Ok,

One more strange question tonight... (I hope)

Can someone help me find a way to convert the following var correctly?

$var='tHIS iS A rATHER sTRANGE dATA sTREAM';

I need a way to convery it on the fly from:

tHIS iS A rATHER sTRANGE dATA sTREAM
to
This Is a Rather Strange Data Stream


If I use s/[A-Z]/[a-z]/ then all caps are dropped to lowercase.
Not the answer I'm fishing for. Can someone assist with a line which
will
simple switch LOWER and upper case?

Thank again:
Oldbitcollector

Use tr/// if all you want to do is switch u<->l.
But if you want to controll the beginning of the sentence
or certain single stand alone chars, this might do it:

use strict;
use warnings;

my %SingleCaps = ('a' => '');
my $var = "a real funky one: THIS iS A rATHER sTRANGE dATA sTREAM I
will not forget";

$var =~ s/(^\S+)|(\S+)/
if (defined $1) {"\u\L$1"} # Always cap first word of sentence
?
elsif (length($2) == 1 && exists $SingleCaps{lc($2)}) {
"\l$2"
} else {
"\u\L$2"
}/eg;


print $var,"\n";

__END__

output:
A laeR yknuF enO: sihT sI a rehtaR egnartS ataD maertS I lliW toN
tegroF
huh?
 
D

Dr.Ruud

Eric J. Roode:
Dr.Ruud:

That does not do what the OP claimed to want to do.

That has already been adressed several times:
<
Text::Capitalize will produce the example output from the example input.
My reaction implicitly questions why this questionable implicit
titlecasing example was chosen to illustrate the question, which other
reactions did too.
 
R

robic0

Eric J. Roode:

That has already been adressed several times:
<
Text::Capitalize will produce the example output from the example input.
My reaction implicitly questions why this questionable implicit
titlecasing example was chosen to illustrate the question, which other
reactions did too.
What another module for overhead? Never heard of "titlecasing", it
can't be that easy. Someday when you want to make binaries you
may realize the overhead these modules outside the standard
distribution incurr.
 
R

robic0

What another module for overhead? Never heard of "titlecasing", it
can't be that easy. Someday when you want to make binaries you
may realize the overhead these modules outside the standard
distribution incurr.
Ooops, I stand by that even though the if invoked the modules in
standard incurr the same overhead. But I don't use them if at all
possible because of initialization and deconstruction. And most
importantly, they are mostly crap.
 
E

Eric J. Roode

[rant deleted]

Gah. I shouldn't have flown off the handle like that. It has been a
stressful day. I apologize, MrReallyveryynice.

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top