simplify arguments for mig-skip-iconv and mig-skip-clean
[migration-tools.git] / compile_zips
1 #!/usr/bin/perl -w
2
3 # Copyright 2009-2012, Equinox Software, Inc.
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
19 use strict;
20
21 # Given input like "Miami Springs\tFL\t33166\n" derived from patron addresses,
22 # this utility will print a city and state for each zip that has the maximum
23 # number of occurrences. (It does not attempt to break ties. If there is a tie,
24 # the city and state that reaches the maximum first will end up winning.)
25
26 my %zips;
27
28 # Go through the input and tally the city-state combinations for each ZIP code
29 while (<>) {
30         chomp;
31         (my $city, my $state, my $zip) = split(/\t/) or next;
32         next unless $zip =~ m/([\d]{5})/; # If it doesn't have 5 digits in a row, it's not a ZIP
33         $zip =~ s/^([\d]{5}).*$/$1/;      # We only want the 5-digit ZIP
34         $state = uc($state);
35         $zips{$zip}{"$city\t$state"}++;
36 }
37
38 # Pick and print a winner for each ZIP code
39 foreach(sort keys %zips) {
40         my $zip = $_;
41         my $max = 0;
42         my $citystate = "";
43         foreach(keys %{$zips{$zip}}) {
44                 if ($zips{$zip}{$_} > $max) {
45                         $max = $zips{$zip}{$_};
46                         $citystate = $_;
47                 }
48         }
49         print "$citystate\t$zip\n";
50 }