Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Lower casing letters of words

Reply
Thread Tools

Lower casing letters of words

 
 
laredotornado@zipmail.com
Guest
Posts: n/a
 
      07-15-2005
Hello,
Is there a simple perl command line expression I can write that will
take

"CORDOVA SCHOOL DISTRICT"

and make it

"Cordova School District"

The rule I'm using is that I'm lower casing every letter of that is
not the first of each word.


Ok, if that question was easy to answer, I have a slightly harder one.
Is there an expression I can write that will apply the above logic to a
certain column of a CSV file? Let's say I want to apply that to the
4th column of a file with a line like

,,"00000749","CORDOVA SCHOOL DISTRICT","PO BOX
140","CORDOVA","AK","99574","0140","907","424","32 65","100 FISHERMAN
AVENUE","CORDOVA",
"AK","99574",510.00,36.43,-1,12,,"05","00000749","CORDOVA SCHOOL
DISTRICT",,"AKCORDOVA"

Any ideas? Thanks, - Dave

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      07-15-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hello,
> Is there a simple perl command line expression I can write that will
> take
>
> "CORDOVA SCHOOL DISTRICT"
>
> and make it
>
> "Cordova School District"


s/(\w+)/\u\L$1/g;

Search for a word, and save it. Replace with that word, after it has
been transformed to all lowercases and had its first letter transformed
to uppercase.


>
> The rule I'm using is that I'm lower casing every letter of that is
> not the first of each word.
>
>
> Ok, if that question was easy to answer, I have a slightly harder one.
> Is there an expression I can write that will apply the above logic to a
> certain column of a CSV file? Let's say I want to apply that to the
> 4th column of a file with a line like
>
> ,,"00000749","CORDOVA SCHOOL DISTRICT","PO BOX
> 140","CORDOVA","AK","99574","0140","907","424","32 65","100 FISHERMAN
> AVENUE","CORDOVA",
> "AK","99574",510.00,36.43,-1,12,,"05","00000749","CORDOVA SCHOOL
> DISTRICT",,"AKCORDOVA"
>
> Any ideas? Thanks, - Dave


There are several CSV parsers and maniuplators available. Take a look
at Text::CSV for starters.

Paul Lalli

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      07-15-2005
Paul Lalli wrote:
> (E-Mail Removed) wrote:
> > Hello,
> > Is there a simple perl command line expression I can write that will
> > take
> >
> > "CORDOVA SCHOOL DISTRICT"
> >
> > and make it
> >
> > "Cordova School District"

>
> s/(\w+)/\u\L$1/g;


Bah. Should have checked the Perl FAQ before posting. Of course, so
should the OP...

perldoc -q capitalize
shows that this solution is sub-optimal at best, and recommends a
slightly longer alternative. I recommend you use the one suggested
there.

> Paul Lalli


 
Reply With Quote
 
laredotornado@zipmail.com
Guest
Posts: n/a
 
      07-18-2005
Hello, Thanks for your response. Was this meant to go in a script? I
created a script

#!/bin/ksh

while read -C -a array; do
array[3]=`echo ${array[2]} | perl -pi -e 's/(\w+)/\u\L$1/g'
myfile.txt`
array[4]=`echo ${array[4]} | perl -pi -e 's/(\w+)/\u\L$1/g'
myfile.txt`
array[5]=`echo ${array[5]} | perl -pi -e 's/(\w+)/\u\L$1/g'
myfile.txt`
echo "${array[*]|,,}"
done

Then I ran

../temp.sh

but got the error

../temp.sh[3]: read: bad option(s)

What am I doing incorrectly? Thanks, - Dave

Paul Lalli wrote:
> Paul Lalli wrote:
> > (E-Mail Removed) wrote:
> > > Hello,
> > > Is there a simple perl command line expression I can write that will
> > > take
> > >
> > > "CORDOVA SCHOOL DISTRICT"
> > >
> > > and make it
> > >
> > > "Cordova School District"

> >
> > s/(\w+)/\u\L$1/g;

>
> Bah. Should have checked the Perl FAQ before posting. Of course, so
> should the OP...
>
> perldoc -q capitalize
> shows that this solution is sub-optimal at best, and recommends a
> slightly longer alternative. I recommend you use the one suggested
> there.
>
> > Paul Lalli


 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      07-18-2005
Top posting corrected - please don't top-post.

(E-Mail Removed) wrote:
> Paul Lalli wrote:
>
>> Paul Lalli wrote:
>>
>>> (E-Mail Removed) wrote:
>>>
>>>> Hello, Is there a simple perl command line expression I can
>>>> write that will take
>>>>
>>>> "CORDOVA SCHOOL DISTRICT"
>>>>
>>>> and make it
>>>>
>>>> "Cordova School District"
>>>
>>> s/(\w+)/\u\L$1/g;

>>
>> Bah. Should have checked the Perl FAQ before posting. Of course,
>> so should the OP...
>>
>> perldoc -q capitalize shows that this solution is sub-optimal at
>> best, and recommends a slightly longer alternative. I recommend
>> you use the one suggested there.
>>

>
> Hello, Thanks for your response. Was this meant to go in a script?
> I created a script
>
> #!/bin/ksh
>
> while read -C -a array; do
> array[3]=`echo ${array[2]} \
> | perl -pi -e 's/(\w+)/\u\L$1/g' myfile.txt`
> array[4]=`echo ${array[4]} \
> | perl -pi -e 's/(\w+)/\u\L$1/g' myfile.txt`
> array[5]=`echo ${array[5]} \
> | perl -pi -e 's/(\w+)/\u\L$1/g' myfile.txt`
> echo "${array[*]|,,}"
> done
>
> Then I ran
> ./temp.sh
> but got the error
> ./temp.sh[3]: read: bad option(s)
>
> What am I doing incorrectly?


That's a Korn shell error not a Perl error. Try `man ksh`. This
newsgroup (comp.lang.perl.misc) is not a good place to ask questions
about Korn shell. I don't have Korn shell to hand but you can probably
do what you want purely using Korn shell without resorting to Perl.

OTOH you can probably solve your problem purely in Perl without
resorting to ksh. I'd do that. YMMV.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to convert capital letters to lower case Chris Mayhew NZ Computing 25 01-25-2012 04:08 PM
Search function - upper and lower letters Omicron Javascript 2 02-10-2008 08:05 PM
making all letters Caps/Small Letters Merrigan Python 4 12-14-2007 10:10 AM
OptionParser: using upper AND lower case option letters timm Ruby 1 05-09-2005 03:10 PM
Upper and Lower case letters Ch. Rajinder Nijjhar Jatt Computer Support 5 11-02-2003 05:16 PM



Advertisments