while () { m/(.*)\s+(\d+)/; $hash{$1} += $2; #$1 is the fruit $2 the number } for my $fruit (keys %hash) { print "$fruit $hash{$fruit}\n"; } __END__ Apple Grape 100 Ginger Fry 200 Apple Grape 80 Ginger Banana 800 Ginger Fry 150 Ginger Banana 45