11 elect_zips - Utility to elect a winning city/state for each ZIP code based on patron data
15 psql -U evergreen -A -t -F $'\t' -c 'SELECT city, state, post_code FROM actor.usr_address' > raw-csz.tsv
16 elect_zips < raw-csz.tsv > winning-zips.tsv
20 Given input like "Miami Springs\tFL\t33166\n" derived from patron addresses,
21 this utility will print a city and state for each zip that has the maximum
22 number of occurrences. (It does not attempt to break ties. If there is a tie,
23 the city and state that reaches the maximum first will end up winning.)
25 You can also feed the output of elect_zips directly into I<enrich_zips --db US.txt --makezips>
31 # Go through the input and tally the city-state combinations for each ZIP code
34 (my $city, my $state, my $zip) = split(/\t/) or next;
35 next unless $zip =~ m/([\d]{5})/; # If it doesn't have 5 digits in a row, it's not a ZIP
36 $zip =~ s/^([\d]{5}).*$/$1/; # We only want the 5-digit ZIP
40 $zips{$zip}{"$city\t$state"}++;
43 # Pick and print a winner for each ZIP code
44 foreach(sort keys %zips) {
48 foreach(keys %{$zips{$zip}}) {
49 if ($zips{$zip}{$_} > $max) {
50 $max = $zips{$zip}{$_};
54 print "$citystate\t$zip\n";