The goal of this perl function is to check if a particular postal code falls into a a number of "bad" postal code ranges
## Deliverables
The goal of this perl function is to check if a particular postal code falls into a a number of "bad" postal code ranges
Canadian postal code follow a format like "A0A 0A0". See wikipedia for more info [login to view URL]
The range of bad postal code is explained here: [login to view URL]
This table represent ranges of the three first character of the postal code. This means we completly ignore the the last three characters of the postal code. We only use the first three to check in they are in a bad range.
So the first example is A0A-A0Z this means any postal that begin with A0 is bad.
The ranges are saved in a mysql table in 6 columns (f1,f2,f3,f4,f5,f6).
I've been using this code which works most of the time. However it doesnt work properply for range like V1Z-V2R. Trying to match V2K gives me no match when in fact it is within that range.
sub Check_for_ups_surcharge {
my ($postalcode, $dbh) = @_;
my ($f1,$f2,$f3) = split(//,$postalcode); # we only keep the three first characters
my $sth = $dbh->prepare("SELECT CONCAT(f1,f2,f3,'-',f4,f5,f6)
FROM surcharge_area_ups
WHERE f1 LIKE '$f1'
AND '$f2' BETWEEN f2 AND f5
AND '$f3' BETWEEN f3 and f6
LIMIT 1
");
$sth->execute or die ("Couldn't execute: " . DBI->errstr);
my ($surcharge_code) = $sth->fetchrow_array;
my $surcharge = "No";
$surcharge = "<b><font color=red>yes</font></b>" if ($surcharge_code);
return($surcharge);
}
So the job is create a little bit of perl/SQL to correctly check if a postal code is within one of the bad range.