#!/usr/bin/perl use strict; use warnings; my %country_hash; while () { next if /^(\s*(#.*)?)?$/; # skip blank lines and comments chomp; # lines that are not skipped remove new line character '\n' my($country,$number) = (split /:/)[0,-1]; # split line on column ':' push @{$country_hash{$country}},$number; } foreach my $country (sort keys %country_hash) { print "$country:", join (":",@{$country_hash{$country}}), "\n"; } =Prints China:2:2:70 Japan:6:10 Thailand:6 =cut __DATA__ China:wd:2 Japan:wd:6 China:sg:2 Japan:sg:10 China:kng:70 Thailand:kng:6