$ cat country
Chicago, USA
Frankfurt, Germany
Berlin, Germany
Washington, USA
Helsinki, Finland
New York, USAsql
- #!/usr/bin/perl -w
- use strict;
- my %table;
- my ($city,$country);
- while (<>) {
- chomp;
- my ($city,$country) = split /,/;
- push @{$table{$country}},$city;
- }
- foreach $country (sort keys %table){
- print "$country:";
- my @cities = @{$table{$country}};
- print join ',', sort @cities;
- print ".\n";
- }
result:ide
$ perl country.pl country
Finland:Helsinki.
Germany:Berlin,Frankfurt.
USA:Chicago,New York,Washington.spa