my @B_copy = @B; ... while (@B_copy) { build_hash(\%result, \@A, \@B_copy, $reverse); ...