Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Lower casing letters of words (http://www.velocityreviews.com/forums/t893313-lower-casing-letters-of-words.html)

laredotornado@zipmail.com 07-15-2005 03:35 PM

Lower casing letters of words
 
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


Paul Lalli 07-15-2005 03:52 PM

Re: Lower casing letters of words
 
laredotornado@zipmail.com 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


Paul Lalli 07-15-2005 03:53 PM

Re: Lower casing letters of words
 
Paul Lalli wrote:
> laredotornado@zipmail.com 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



laredotornado@zipmail.com 07-18-2005 06:32 PM

Re: Lower casing letters of words
 
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:
> > laredotornado@zipmail.com 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



RedGrittyBrick 07-18-2005 07:15 PM

Re: Lower casing letters of words
 
Top posting corrected - please don't top-post.

laredotornado@zipmail.com wrote:
> Paul Lalli wrote:
>
>> Paul Lalli wrote:
>>
>>> laredotornado@zipmail.com 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.


All times are GMT. The time now is 10:13 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.