#!/usr/bin/perl -w use strict; my %stuff; while() { chomp; my ( $country,$state,$city) = split /:/; push @{$stuff{$country}->{$state}},$city; } foreach my $country ( keys %stuff ) { print "The states in $country are ",join ",",keys %{$stuff{$country}},"\n"; foreach my $state ( keys %{$stuff{$country}} ) { print "The cities in $state are ", join ",", @{$stuff{$country}->{$state}},"\n"; } } __DATA__ USA:CA:Los Angeles USA:CA:San Francisco USA:NY:New York CAN:Alb:Edmonton CAN:Alb:Calgary