(
'Chicago' => 'USA',
'Frankfurt' => 'Germany',
'Berlin' => 'Germany',
'Washington' => 'USA',
'Helsinki' => 'Finland',
'New York' => 'USA',
)
####
(
'Finland' => [ 'Helsinki' ],
'Germany' => [ 'Berlin', 'Frankfurt' ],
'USA' => [ 'Chicago', 'New York', 'Washington' ],
)
##
##
use strict;
use warnings;
# Hash of countries by city
my %countries_by = (
'Chicago' => 'USA',
'Frankfurt' => 'Germany',
'Berlin' => 'Germany',
'Washington' => 'USA',
'Helsinki' => 'Finland',
'New York' => 'USA',
);
# Hash of arrays of cities by country
my %cities_by;
# Generate a hash of arrays of cities by country
# from a hash of countries by city...
while (my ($city, $country) = each %countries_by) {
push @{ $cities_by{$country} }, $city;
}
# Print a list of cities by country...
for my $country (sort keys %cities_by) {
for my $city (sort @{ $cities_by{$country} }) {
print "$country\t$city\n";
}
}