my %totals; @totals{$CASH, $ACCOUNT, $CHECK, $GIFT, $VOUCHER, $CC_MAN_AUTH} = \($tcash, $taccount, $tcheck, $tgift, $tvoucher, $tcc_man_auth); while ( my $data = $t_sth->fetchrow_arrayref ) { my ( $amt, $id ) = @$data; $amt /= PRECISION; if (my $totalvar = $totals{ $id }) { $$totalvar += $amt; } else { $tcredit += $amt; } }