i feel kinda retarted for haveing to use 2 substr but i cant think of another way to do the inline edit of $2 at the moment.
#!/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";
}
}
The below is what the perl group said.
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, 0, " ") if length $data[11] ==
7;
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.
-Jim......