#!/perl/bin/perl
#use strict;
print "Content-type: text/html\n\n";
print "
Zip Code Lookup Results
\n";
$debug=0;
if ($ENV{'CONTENT_LENGTH'} == 0) {
$ENV{'CONTENT_LENGTH'} = 62;
$debug = 1;
}
print "L15 - $ENV{'CONTENT_LENGTH'}
\n";
$argcnt=0;
read(STDIN, $input, $ENV{'CONTENT_LENGTH'});
if ($debug) {
print "Input string:$input
";
print "L23 - $ENV{'CONTENT_LENGTH'}
\n";
}
@input = split(/&/,$input);
PARSE:
for ($i=0;$i<=$#input;$i++){
($g, $z) = split(/=/, $input[$i],2);
# $z =~ s/ //g;
last PARSE if ($z=="");
$ARGV[$i] = $z;
$argcnt++;
}
$argcnt--;
#print "LINE 35 - argcnt=$argcnt
";
# $zlist = split(/=/,@input);
$stime=time();
@ziplist = ($z1, $z2);
print "L44 - $ziplist[0] $ziplist[1]
";
$stime=time();
#$ARGV[0]=$z1;
#$ARGV[1]=$z2;
for ($i=0; $i<= $argcnt;$i++){
#print "In find loop: $i\n";
($zip[$i], $state[$i], $city[$i], $lat[$i], $long[$i]) = findzip($ARGV[$i]);
$city[$i] =~ s/^ +//g; #match one or more spaces at the start of a line.
# print "$zip[$i], $city[$i], $state[$i], $lat[$i], $long[$i]\n
";
if ($lat[$i] == 0 || $long[$i] == 0){
print "Zip $zip[$i] is missing location
information
\n";
}
}
for ($i=0; $i <= $argcnt; $i++){
$dest=$i+1;
if ($i == $argcnt) {$dest=0;}
$dst[$i] = dist($lat[$i], $long[$i], $lat[$dest], $long[$dest]);
if ($lat[$i] == 0 || $long[$i] == 0 || $lat[$dest] == 0 ||
$long[$dest] == 0) {
$dst[$i] = "Unknown distance";
} else {
#print "$dst[$i]\n";
$dst[$i] =~ s/\..+/ miles/;
#print "$dst[$i]\n";
}
# print "$city[$i], $state[$i] $zip[$i] $lat[$i] $long[$i] to
#$city[$dest], $state[$dest] $zip[$dest] $lat[$dest] $long[$dest] :";
print "L 85 - $city[$i], $state[$i] $zip[$i] to $city[$dest],
$state[$dest] $zip[$dest] : ";
print "$dst[$i] (lat $lat[$i] long $long[$i])****$dst[$i]****
";
}
print "L90 -
\n";
$etime=time();
elapsed() ;
print "Return to Zip Code Lookup
\n";
#print "Go to ZIP!
\n";
exit;
$linecnt=1; #loop counter, how many have we looked at?
$fnd=-1; #how many have we found?
SEARCH:
while ($line = ) {
$thiszip = substr($line, 0, 5);
$i=0;
while ($ARGV[$i]){
if ($thiszip == $ARGV[$i]){
$short = substr($line, 0, 50);
print "line #: $linecnt Found Zip: $short";
chomp $line;
($zip[$i], $state[$i], $name[$i], $lat[$i], $long[$i]) =
split /,/,$line,5;
@fullline[$i] = $line;
$fnd++;
last SEARCH if($fnd==$#ARGV) ;
}
$i++;
}
$linecnt++;
}
print "\n\n";
$i=0;
for ($i=0; $i <= $#ARGV; $i++){
$dest=$i+1;
if ($i == $#ARGV) {$dest=0;}
$dst[$i] = dist($lat[$i], $long[$i], $lat[$dest], $long[$dest]);
print "$name[$i], $state[$i] to $name[$dest], $state[$dest]: $dst[$i] miles \n";
}
exit;
sub elapsed {
# etime set after search. $etime=time();
#print "---------------------------------------------------------------\n";
#print "start = $stime end = $etime
\n";
print "Elapsed time = ";
print $etime - $stime;
print "
\n";
}
sub acos { atan2( sqrt(1-$_[0] * $_[0]), $_[0]) }
sub dist {
$pi = atan2(1,1) * 4;
my @parms = @_;
my $lat1 = $parms[0]/180 * $pi ;
my $long1 = $parms[1]/180 * $pi;
my $lat2 = $parms[2]/180 * $pi;
my $long2 = $parms[3]/180 * $pi;
# print "$lat1, $long1, $lat2, $long2 \n";
$a = $long1 - $long2;
if ($a < 0) {$a = -$a;}
if ($a > $pi) {$a = 2 * $pi;}
$d = acos(sin($lat2) * sin($lat1) + cos($lat2)*cos($lat1)*cos($a)) * 3958;
return $d ;
}
#########################################################################
#This findzip is awesome...uses an index, and works okay. It is fast and
#cool. Next issue: Can we do without the index?
sub findzipX {
my @parms = @_;
my $z = $parms[0];
open(ZIP, "zip2.txt") ;
open(NDX, "zip.ndx") ;
$keysize=11; #how long is the key
$zipsize=52; #how long is a zip code record
$zipcnt=42730; #how many zip codes are in file
$max = $zipcnt;
$min = 0;
$cand="";
$itcnt=0;
while ($cand != $z and itcnt < 100){
#set a search value
#read it
#key is either greater, lesser, or equal
$search=int($min + (($max-$min)/2));
seek NDX, $search*$keysize, 0;
read NDX, $line,$keysize;
($cand, $key) = split /:/,$line ;
if ($cand > $z) {
$max= $search;
#$min= $min;
} elsif ($cand < $z){
#$max= $max;
$min= $search;
}
print "Cnt=$itcnt zip=$cand key=$key search=$search max=$max min=$min\n";
$itcnt++;
}
# print "Cnt=$itcnt zip=$cand key=$key search=$search max=$max min=$min\n";
# print "\n";
# print "key=$key\n";
seek ZIP, ($key*$zipsize), 0;
read ZIP, $line, $zipsize;
# print "In findzip $z=$line\n";
return split /,/,$line;
}
##########################################################################
sub test {
return "Test1","test2";
}
#########################################################################
#this is a copy of the original findzip (now findzipx)...I am trying to do
#without the index file.
sub findzip {
my @parms = @_;
my $z = $parms[0];
#print "Looking for $z\n";
# open(ZIP, "zip2.txt") ;
# open(NDX, "zip.ndx") ;
#now try the 'normal' zip.txt file, no indexing, no optimization.
open(ZIP, "zip.txt");
$keysize=11; #how long is the key
$zipsize=52; #how long is a zip code record
$zipcnt=42730; #how many zip codes are in file
#the technique is similar...
$zipsize = 1698323; # of bytes in zip.txt
#everything now refers to bytes, rather than records
$max = $zipsize;
$min = 0;
$cand="";
$itcnt=0;
$search=int($min + (($max-$min)/2));
# print "Cnt=$itcnt zip=$cand key=$key search=$search max=$maxmin=$min\n";
while ($cand != $z){
#set a search value
#read it
#key is either greater, lesser, or equal
$search=int($min + (($max-$min)/2));
if ($itcnt > 100) {
@alist = ($z,"Not Found","Not Found","","");
@alist;
return;
}
#print "In loop search $search max $max min $min itcnt $itcnt\n";
seek ZIP, $search, 0;
# read NDX, $line,$keysize;
#get rid of a partial line because we probably seek'ed into the middle of a record
$line = ; #partial line
$line = ; #read a full line
($cand, $key) = split /:/,$line,2 ; #just first two fields
if ($cand > $z) {
$max= $search + 100; #plus 100, just to make sure we don't lose a line
# $min= $min;
} elsif ($cand<$z){
# $max= $max;
$min= $search - 100;
}
# print "Cnt=$itcnt zip=$cand key=$key search=$search max=$max
#min=$min\n";
$itcnt++;
}
# print "Cnt=$itcnt zip=$cand key=$key search=$search max=$max min=$min\n";
# print "\n";
# print "key=$key\n";
#--- seek ZIP, ($key*$zipsize), 0;
#--- read ZIP, $line, $zipsize;
# print "In findzip $z=$line\n";
return split /,/,$line;
}