in reply to Zipcode search engine

At this point, I'm merely numerically comparing the user's zipcode to each one in the database and determining whether to return or discard each record. Is there a better way to do this?

Probably. This sounds like you are using a flat file 'data base' and simply iterating from the beginning of this to the end. For a small file this works fine but breaks down when you get bigger and or demand on access to this file becomes high. On way to arrange this in a proper database would be to have two tables:

Zip Table Zip Code,Distrubitor Code/s 71000, 1|2|3 72000, 3|4 .... Distributor Table Dist ID, Lotsa other Data :-) 1, She'll be right INC 2, No Worries Mate INC 3, Can Do Corp 4, The Impossible Done Yesterday P/L

You do the lookup on the Zip table (really basic SQL) to get the Dist ID(s) for that area (Pipe delimited here). You then grab all those Distributors from the Dist table. Presumbaly the Distributor ID table should already exist? So you just need to generate a Zip table that cross references the Zip code to the Dist ID. You could do this manually using a map or automate it using the zip code geographic position data mentioned elsewhere. You would probable need to get geographic co-ordinates for you distributors but this is pretty easy. You can get the longs and lats for any US city from many of the online mapping services free. See jcwren monk map page (on the stats pages) for one such link.

You could implement this using DBI and DBD::CSV making it easy to move to a real database in the future.

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Zipcode search engine
by mandog (Curate) on Sep 10, 2001 at 06:24 UTC
    A possible problem with tachyon's code is that it doesn't work unless you happen to be in a zip code with distributors

    If you call call the UPSPS National Customer Support Center at (800) 238-3150 you can talk to the them about ordering zip/latitude/longitude data

    The documentation for this data is available as a pdf on the usps site.

    It seems like most people find a vendor to give them this data.The USPS FAQ talks about it a bit.

    I'm guessing that the 'ol a**2 + b**2 =c**2 would be how you would compute the shortest distance between two arbitrary zip codes. No doubt the monk map uses nicer code...

    use strict; use warnings; my $x1=10; my $y1=20; my $x2=-8; my $y2=2; my $dist=sqrt(($x1-$x2)**2+($y1-$y2)**2); print "$dist =distance";
    Of course you have to (pre?) compute the distance between the user's zip code and many of the other zip codes in your database...



    --mandog

Re: Re: Zipcode search engine
by ralphie (Friar) on Sep 10, 2001 at 04:14 UTC
    wouldn't it be preferable to adhere to normalization rules and have the zipcode database in the following form?

    zip distributor
    71000 0001
    71000 0002
    71000 1953
    71001 1113
    71004 0006