# Assume the following input delimited be a newline Chicago, USA Frankfurt, Germany Berlin, Germany Washington, USA Helsinki, Finland New York, USA 1 while (<>) { 2 chomp; 3 my ($city, $country) = split /, /; 4 push @{$table{$country}}, $city; 5 } 6 7 foreach $country (sort keys %table) { 8 print "$country: "; 9 my @cities = @{$table{$country}}; 10 print join ’, ’, sort @cities; 11 print ".\n"; 12 } In the first part, line 4 is the important one. We’re going to have a hash, %table, whose keys are country names, and whose values are (references to) arrays of city names. After acquiring a city and country name, the program looks up $table{$country}, which holds (a reference to) the list of cities seen in that country so far.