#!/usr/local/bin/perl -w use strict; my %numbers = ( '210' => 'rg1000', '212' => 'rg1000', '214' => 'rg1000', '215' => 'rg1000', '218' => 'rg1003', '221' => 'rg1000', '222' => 'rg1003', '223' => 'rg1003', '224' => 'rg1003' ); # if the first 2 digit match and point to the same rg then print only the first two digits with the rg foreach my $item (keys %numbers) { print "$item\n"; } #desired output # 21 => rg1000 # 218 => rg1003 # 22 => rg1003 # 221 => rg1000