First you might also try perusing perldsc for good info on Hashes of Hashes, Arrays of Arrays, Arrays of Hashes,etc.

If the files are not all that big (to justify using a database, as I would do if the files were big, and then run an SQL Query), or I wanted a little practice honing up my hashes of hashes skills I might do the following (I have removed some code to let you work out some details, this is probably not the optimal way but it is one way):

My logic goes as follows:

1. open <FILE2> and place all the cities in a hash keyed by zipcode something like:

chomp($line) #line from FILE2 my ($zip_code,$city) = split /\|/,$line; $zip_city{$zip_code} = $city; #insert city into hash

2. open <FILE1> again split it and compare the keys in my original hash setting the state and city in a hash again keyed against zip (the delete $zip_city{$current_zipcode}; part is so that I don't continue to go over zips I already have a state for, you could also put a last; so it would not continue reading from <FILE1> once the hash %zip_city contained no keys).

chomp($line); my ($state,$min,$max) = split /,/,$line; foreach my $current_zipcode ( keys %zip_city ) { if ( $current_zipcode >= $min and $current_zipcode <= $max ) { $zip_city_state{$current_zipcode}{city} = $zip_city{$current_zipc +ode}; $zip_city_state{$current_zipcode}{state} = $state; delete $zip_city{$current_zipcode}; } }
3. Lastly for each line in <FILE3> I would see if the zipcode (assuming you put it in a variable called $zipfrom3)existed in the second hash (%zip_city_state hash) and if it does print the info you want:
print "$zipfrom3,$zipzip_city_state{$zipfrom3}{city}, $zip_city_state{ +$zipfrom3}{state}\n";
or else complain that it did not exist in the hash (if you use warnings; it will do a good job of complaining if you don't check for the existance of a zip in the hash first, but you should check for the existance anyhow).

-enlil


In reply to Re: How to match zip code to city,state by Enlil
in thread How to match zip code to city,state by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.