[comp.lang.perl] Your own zipcode database

merlyn@iwarp.intel.com (Randal L. Schwartz) (12/11/90)

Instructions for your very own zipcode <=> nameplace database...

(1) go over to comp.binaries.ibm.pc, and fetch the 10 parts of zipcode.zoo.

(2) extract them with your favorite multipart uudecoder.  I use this one:

##################################################
#!/local/usr/bin/perl
while (<>) {
	$sawbegin++, last if ($mode,$file) = /^begin\s*(\d*)\s*(\S*)/;
}
die "missing begin" unless $sawbegin;
open(OUT,"> $file") if $file ne "";
while (<>) {
	$sawend++, last if /^end/;
	next if /[a-z]/;
	next unless int((((ord() - 32) & 077) + 2) / 3) == int(length() / 4);
	print OUT unpack("u", $_);
}
die "missing end" unless $sawend;
chmod oct($mode), $file;
exit 0;
##################################################

[Thanks to lwall for most of this uudecode program.]

(3) use "zoo" to extract "zipcode.dbf" from "zipcode.zoo".  (I can't
remember where I got "zoo", but it's out there somewhere. :-)

(4) run the following Perl code:

##################################################
#!/local/merlyn/bin/perl

$recfmt = "a5 A15 a2 x1";
$reclen = length(pack($recfmt,()));  print "length = $reclen\n";

open(Z, "zipcodes.dbf") || die;
seek(Z,130,0);
while(read(Z,$rec,$reclen)) {
	($zip,$city,$state) = unpack($recfmt,$rec);
	print "$zip $city $state\n";
}
close(Z);
##################################################

(5)  Enjoy.  Season to taste.

print "Just another Perl hacker (with a bit of zip),"
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Intel: putting the 'backward' in 'backward compatible'..."====/