[Chicago-talk] zipcode
Steven Lembark
lembark at wrkhors.com
Sun Feb 29 12:56:24 CST 2004
-- "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 (/(\w*,\w*,\w*,\w*,\w*,\w*,\w*,\w*,\w*,\w*,\w*,)(\w*)(.*)/) {
> 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";
> }
> }
Don't use a regex to split up the data on commas for one:
use split. Then take the 12th field and play with it. If
you need to then rejoin the data:
my $sepchar = ',';
my $regex /$sepchar/o;
...
local $\ = '';
local $, = $sepchar;
for( @input );
{
my @fieldz = split $regex;
$fieldz[12] =~ s/^(.{4} /$1 /
if length $fieldz[12] == 7;
# never chopmed it, the newline should
# still be at the end.
print @fieldz;
}
--
Steven Lembark 2930 W. Palmer
Workhorse Computing Chicago, IL 60647
+1 888 359 3508
More information about the Chicago-talk
mailing list