[ic] Getting UPS Zone
Bob Puff
bob at nleaudio.com
Mon Sep 4 21:24:13 EDT 2006
Ok, I'll answer my own question, for the benefit of the schmucks like
myself who will be searching the archives for a solution.
I needed something that would calculate days in transit. What I really
only needed was the UPS Zone (for the lower 48 only). Turns out, most
of the code I needed was in Ship.pm. I extracted the necessary stuff,
and made it a tag. Here it is, in all of its unoptimized, wrapped glory:
UserTag findzone Order dstzip
UserTag findzone Routine <<EOR
sub make_three {
my $zx = shift;
while ( length($zx) < 3 ) {
$zx = "0$zx";
}
return $zx;
}
sub {
# Find the zone based on a zip code, and return the first column (GND UPS)
# Hard-coded Zone file, modify as needed:
my $fn="/home/nystyle/catalogs/deli/products/144.csv";
my ($dstzip) = @_;
my $ref;
$ref->{zone_data}=readfile($fn);
unless ($ref->{zone_data}) {
return 0; #can't read zone file
}
my (@zone) = grep /\S/, split /[\r\n]+/, $ref->{zone_data};
shift @zone while @zone and $zone[0] !~ /^(Postal|Dest.*Z)/;
if($zone[0] =~ /^Postal/) {
$zone[0] =~ s/,,/,/;
for(@zone[1 .. $#zone]) {
s/,/-/; }
}
if($zone[0] !~ /\t/) {
my $len = 3;
@zone = grep /\S/, @zone;
@zone = grep /^[^"]/, @zone;
$zone[0] =~ s/[^\w,]//g;
$zone[0] =~ s/^\w+/low,high/;
@zone = grep /,/, @zone;
$zone[0] =~ s/\s*,\s*/\t/g;
for(@zone[1 .. $#zone]) {
s/^\s*(\w+)\s*,/make_three($1, $len) . ',' . make_three($1,
$len) . ','/e;
s/^\s*(\w+)\s*-\s*(\w+),/make_three($1,$len) . ',' .
make_three($2, $len) .','/e;
s/\s*,\s*/\t/g;
}
}
my $x=0;
$dstzip=substr($dstzip,0,3);
for(@zone [1..$#zone]) {
my @data=split /\t/, $_;
if ($dstzip ge $data[0] and $dstzip le $data[1]) {
$x=$data[2]; if ($x=="-") { $x=0; }
last;
}
}
return $x;
}
EOR
More information about the interchange-users
mailing list