For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > February 2007 > converting to mixed case









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author converting to mixed case
Jm

2007-02-26, 7:00 pm

is there a function, module, script, etc. that converts all uppercase to
proper mixed case. this particular need is for an address list that is all
uppercase

1370 W 14TH ST
ADA,OK
74837

i would like to convert it to

1370 W. 14th St.
Ada, OK
74837

thanks for any help,
joe

--
since this is a gmail account, please verify the mailing list is included in
the reply to addresses

Adriano Ferreira

2007-02-26, 7:00 pm

On 2/26/07, jm <jm5379@gmail.com> wrote:
> is there a function, module, script, etc. that converts all uppercase to
> proper mixed case. this particular need is for an address list that is all
> uppercase


Doing it blindly, can be easily achieved with regexes:

$address =~ s/(\w)(\w*)/\u$1\L$2/g;

which applied over each line of

> 1370 W 14TH ST
> ADA,OK
> 74837


results in

1370 W 14th St
Ada,Ok
74837

which is not perfect, because it is pretty dumb as I said. To do a
better job, you need to sophisticate the recognition of the address
pieces and then apply s/(\w)(\w+)/\u$1/\L$2/ selectively to the parts
(plus your extra tidyings like a trailing dot and extra spaces).

Note. Converting a word into first letter in upper case and the
remaining in lower case, can also be done with s/(\w+)/\L\u$1/


> 1370 W. 14th St.
> Ada, OK
> 74837
>
> thanks for any help,
> joe
>
> --
> since this is a gmail account, please verify the mailing list is included in
> the reply to addresses
>

D. Bolliger

2007-02-26, 7:00 pm

jm am Montag, 26. Februar 2007 18:54:

Hi

> is there a function, module, script, etc. that converts all uppercase to
> proper mixed case. this particular need is for an address list that is all
> uppercase


This description of what you want to achieve does not correspond to the
example below; you also add '.'s and a space, and 'OK' is still non-mixed
case.

> 1370 W 14TH ST
> ADA,OK
> 74837
>
> i would like to convert it to
>
> 1370 W. 14th St.
> Ada, OK
> 74837


you can try to fiddle around with ucfirst and lc, see:

perldoc -f ucfirst
perldoc -f lc

however, some exceptions are to be handled as well as the format kept...
(therefore the map chain below)

The following does some of the tasks (I did not search google...):


#!/usr/bin/perl

use strict;
use warnings;

my $s="
1370 W 14TH ST
ADA,OK
74837
";

print map ucfirst,
map {$_ eq ',' ? ', ' : $_} # handle special case
map {$_ eq 'OK' ? $_ : lc $_ } # dito, instead of unconditional lc
($s=~/(\w+)(\W+)/g); # not very sophisticated :-)

__END__

1370 W 14th St
Ada, OK
74837


Dani
Ken Foskey

2007-02-26, 7:00 pm

On Mon, 2007-02-26 at 11:54 -0600, jm wrote:
> is there a function, module, script, etc. that converts all uppercase to
> proper mixed case. this particular need is for an address list that is all
> uppercase
>
> 1370 W 14TH ST
> ADA,OK
> 74837
>
> i would like to convert it to
>
> 1370 W. 14th St.
> Ada, OK
> 74837



There was a postal module written by a person in Australia somewhere on
cpan, cannot remember name and the details are at work. This may go
somewhere along the road.

This sort of thing is almost exclusively a list based application and
the lists are built up by specialists over time. There are usually huge
tables of spelling differences as well. for example

St, Strt, Street => Street, St

In your case I would have a table of full text states and state codes
and translate them to the actual codes afterwards.

Frankly the best way to deal with this is to build your input
application so that you know what is a state and what is a suburb, you
do have hope of doing something sensible then.

--
Ken Foskey
FOSS developer

John W. Krahn

2007-02-26, 7:00 pm

Adriano Ferreira wrote:
> On 2/26/07, jm <jm5379@gmail.com> wrote:
>
> Doing it blindly, can be easily achieved with regexes:
>
> $address =~ s/(\w)(\w*)/\u$1\L$2/g;


Or more simply:

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



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
Jm

2007-02-26, 7:00 pm

thanks to all for your suggestions. Adriano had exactly what i was looking
for:

$address =~ s/(\w)(\w*)/\u$1\L$2/g;

works like a charm.
i should have left out the periods and uppercase state abbreviations, i'm
sure that didn't help clarify what i wanted.

thanks again.



On 2/26/07, Adriano Ferreira <a.r.ferreira@gmail.com> wrote:
>
> On 2/26/07, jm <jm5379@gmail.com> wrote:
> all
>
> Doing it blindly, can be easily achieved with regexes:
>
> $address =~ s/(\w)(\w*)/\u$1\L$2/g;
>
> which applied over each line of
>
>
> results in
>
> 1370 W 14th St
> Ada,Ok
> 74837
>
> which is not perfect, because it is pretty dumb as I said. To do a
> better job, you need to sophisticate the recognition of the address
> pieces and then apply s/(\w)(\w+)/\u$1/\L$2/ selectively to the parts
> (plus your extra tidyings like a trailing dot and extra spaces).
>
> Note. Converting a word into first letter in upper case and the
> remaining in lower case, can also be done with s/(\w+)/\L\u$1/
>
>
> included in
>




--
since this is a gmail account, please verify the mailing list is included in
the reply to addresses

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com