[Chicago-talk] zipcode

Dooley, Michael Dooley.Michael at con-way.com
Fri Feb 27 10:21:21 CST 2004

you know the first iteration of this script I used split. but I failed to
use join. it didn't even dawn on me to use join. so many different ways to
do 1 task sometimes they get away from you.

-----Original Message-----
From: chicago-talk-bounces at mail.pm.org
[mailto:chicago-talk-bounces at mail.pm.org] On Behalf Of Jim Thomason
Sent: Friday, February 27, 2004 10:18 AM
To: Chicago.pm chatter
Subject: Re: [Chicago-talk] zipcode

When working with long, delimited lists of data like
that, I'm inclined to split it, twiddle it, then
reassemble it.

while (<>) {
 my @data = split /,/;
 #use substr to substitute in a space at the 4th
character if our string is 7 characters long
 substr($data[11], 4, 1, " ") if length $data[11] ==
 print join(',', @data); #no need for a newline, we
have one at the last position

if you want, you could use $, set to ',' and then just
print @data at the end. Might be a little faster, but
I prefer being explicit.

That should work, but I didn't test it, so it may need
a little twiddling.

But you also caught my curiousity...what's with a 7
digit zipcode and what would the space do? I'm only
familiar with 5 digit, 9 digit, and 11 digit (DPBC)
codes (and none of those have spaces). I'm thinking
canadian postal codes, but I'm not sure.

--- "Dooley, Michael" <Dooley.Michael at con-way.com>
> ok if you have a CSV file that has 12+ fields.
> on the 12th field you have a zipcode. if the Zip is
> 7 character put a space
> between the 4th and 5th character.
> I don't know why I am having this brain blockage
> but:
> 1) is there a better way to do the regex below?
> 2) how do I do the inline edit?
> what I have below works but I know there has to be a
> much better way to edit
> the zipcode.
> #!/usr/bin/perl -w
> use strict;
> while (<> ) {
> if
> {
>         if (length($2) == 7) {
>                 my $zipstart=substr($2,0,4);
>                 my $zipend=substr($2,4,3);
>                 print "$1$zipstart $zipend$3\n";
>                 next;
>         }
>         print "$1$2$3\n";
> }
> }
> _______________________________________________
> Chicago-talk mailing list
> Chicago-talk at mail.pm.org
> http://mail.pm.org/mailman/listinfo/chicago-talk

Do you Yahoo!?
Get better spam protection with Yahoo! Mail.
Chicago-talk mailing list
Chicago-talk at mail.pm.org

More information about the Chicago-talk mailing list