Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need most efficient way to write this code that checks zips...

Status
Not open for further replies.

spewn

Programmer
May 7, 2001
1,034
i have a file with a zip code on each line. my script pulls a user's information, including a field that includes a zip code, as well as other elements of an address ($addrField). i need to compare the zip code from that field against each zip code in the file, and note if it matches. i also search a field with a numeric value ($zipField), and i check it against that, too.

the script i have works fine. if i search 1 zip code, everything runs perfect. but once i open it up to the whole db, it seems to overwhelm the script, because it shows 0 that match, not even the 1 from earlier.

anyways, here's the script:

Code:
$ismatch=false;
 open(MYINPUTFILE, 'zipFile.txt');
 while(<MYINPUTFILE>) {
 $line = $_;
 chomp($line);
  if (($zipField eq $line)||(($addrField =~ /$line/))) {$ismatch=true;}
 }

is there a better way to accomplish my goal? Seems to give me incorrect results when the zipFile.txt has thousands of lines. would it be better to put the zips in a variable? then search the variable?

Code:
$ismatch=false;
$zipFile=25685,12549,52365,89655,36415,75896,32548,78512,25025,01458...'; #with thousands of zips
@zparry=(/,/,$zipFile);
 foreach $line (@zparry) {
 chomp($line);
  if (($zipField eq $line)||(($addrField =~ /$line/))) {$ismatch=true;}
 }

appreciate any ideas. thanks!

- g
 
When the users supply the zip code, can they ONLY use digits or a format like zip-ext?

Can they somehow add spaces or other character before after the zip code? ex " xxxxx " or "xxxxx-xxxx "...other iterations. Is your validation checking for these?
$line =~ s/\s+//g;

Would it be easier that the user picks the ZIP code provided from a file, which will eliminate the need to type in a zip code?
 
For comparing the data in the $zipField against all the zip codes in zipFile.txt, I would suggest reading through the text file and storing all the zip codes in a hash with some non-false value (so something other than undef or 0.) The check for a valid zip code would then consist of if (defined $hash{$zipField}).

For the $addrField check, you're going to have to be careful. It's possible any house number with 5+ digits could, in theory, match a zip code. Once you sort out the house numbers problem, you could check any zip codes in $addrField against the hash you created from the text file.
 
rharsh's solution is going to be way faster for large numbers of zips, not only because you do the physical i/o on the file only once, but also because the hash lookup is extremely efficient. exists may be marginally faster than defined too. Assuming that any house number is going to occur before the zip code in an address, you can map the $addrField with a regex with the g modifier to get an array of matching 5-digit numbers from the line, and then just use the last one found in the lookup. Sorry, can't give a tested example, no Perl on this machine [sad]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top