git @ Cat's Eye Technologies Dipple / master perl / collate.pl
master

Tree @master (Download .tar.gz)

collate.pl @masterraw · history · blame

#!/usr/bin/env perl

# SPDX-FileCopyrightText: Chris Pressey, the original author of this work, has dedicated it to the public domain.
# For more information, please refer to <https://unlicense.org/>
# SPDX-License-Identifier: Unlicense

use strict;

my $line;
my $states = {};

while ($line = <STDIN>) {
    chomp $line;
    if ($line =~ /^(.*?),\s*(.*?)$/) {
        my $city = $1;
        my $state = $2;
        $states->{$state} = [] if not defined $states->{$state};
        push @{$states->{$state}}, $city;
    } else {
        die "Badly formed line";
    }
}

foreach my $state (sort keys %{$states}) {
    print "$state:\n";
    foreach my $city (@{$states->{$state}}) {
        print "  $city\n";
    }
}