3 # Copyright 2009-2012, Equinox Software, Inc.
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.
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.
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.
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.)
28 # Go through the input and tally the city-state combinations for each ZIP code
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
35 $zips{$zip}{"$city\t$state"}++;
38 # Pick and print a winner for each ZIP code
39 foreach(sort keys %zips) {
43 foreach(keys %{$zips{$zip}}) {
44 if ($zips{$zip}{$_} > $max) {
45 $max = $zips{$zip}{$_};
49 print "$citystate\t$zip\n";