my %totals; # ... my( $type, $total ) = @{ $row }; $totals{ $type } += $total; #... my $grand_total += $totals{ $_ } for qw( A B C D W ); print "W: $totals{ 'W' }\n"; print "grand total: $grand_total\n";