while(@array) { while (@array2) { my $item = shift @array2; my $result = do_stuff2($item); if ($result == 1) { push @array, $item } elsif ($result == 2) { push @array2, $item } # else done with item, discard. } my $item = shift @array; my $result = do_stuff($item); if ($result == 1) { push @array, $item } elsif ($result == 2) { push @array2, $item } # else done with item, discard. }