foreach my $paymentRow ( @$payments ) { foreach my $salesKey ( keys %$sale_items ) { ### make sure we're applying the right line items ... next if ( $paymentRow->{creditacct} != $sale_items->{$salesKey}{debitacct} ); ## if payment can be applied, ## apply it and delete the row from payments/credits array warn "DELETING ... {$salesKey} " if ( $paymentRow->{amount} == $sale_items->{$salesKey}{amount} and $paymentRow->{ppid} == $sale_items->{$salesKey}{ppid} ); delete $sale_items->{$salesKey} <---- DELETED and undef( $paymentRow ) if ( $paymentRow->{amount} == $sale_items->{$salesKey}{amount} and $paymentRow->{ppid} == $sale_items->{$salesKey}{ppid} ); $sale_items->{$salesKey}{amount} -= $paymentRow->{amount} and undef( $paymentRow ) if ( $paymentRow->{amount} < $sale_items->{$salesKey}{amount} <---- AUTOVIVI and $paymentRow->{ppid} == $sale_items->{$salesKey}{ppid} ); } }