#!/usr/bin/perl -w use strict; $|=1; my @numberList = qw (20 30 50); #you get this from a file my %numberHash = map{$_ => 1}@numberList; my @result; my %sums; while () { chomp; my ($first, $second) = split; if ($numberHash{$first}) { push @result, $_; $sums{$first}+= $second; } } @result = sort{ my($firstA) = split(' ',$a); my($firstB) = split(' ',$b); $firstA <=> $firstB }@result; foreach (@result) { my ($first) = split; print "$_ $sums{$first}\n"; } #not sure what format you want output in =prints 20 23424 332444 20 56756 332444 20 82757 332444 20 83758 332444 20 09347 332444 20 76402 332444 30 91857 364101 30 92785 364101 30 89275 364101 30 90184 364101 50 29349 184748 50 71674 184748 50 83725 184748 =cut __DATA__ 10 23432 20 23424 60 45567 20 56756 30 91857 50 29349 10 93729 80 82374 20 82757 30 92785 50 71674 70 81747 20 83758 30 89275 10 19594 60 09214 20 09347 50 83725 90 91845 20 76402 30 90184