in reply to summarization of list

I'm not sure how you pick the 'rg' for a given two digit prefix. (e.g. Why is it 22 => rg1003 and not 22 => rg1000.) Do you pick the most popular 'rg' for the given prefix? If so, the following will do the trick:

my %pop; ++$pop{substr($_, 0, 2)}{$numbers{$_}} foreach keys %numbers; my %most_pop; foreach my $short (keys %pop) { my $short_pop = $pop{$short}; my ($most_pop) = sort { $short_pop->{$b} <=> $short_pop->{$a} } keys %$short_pop; $most_pop{$short} = $most_pop; } my %filtered; foreach (keys %numbers) { my $key = $_; my $rg = $numbers{$_}; my $short = substr($_, 0, 2); my $most_pop = $most_pop{$short}; if ($rg eq $most_pop) { $filtered{$short} = $most_pop; } else { $filtered{$key} = $rg; } } print("$_ => $filtered{$_}\n") foreach sort keys %filtered;