while(@array) { my $item = shift @array; if (used_to_be_in_array1($item)) { # Do various array1ish things with $item, some of which will _possibly_: push @array, ... } elsif (used_to_be_in_array2($item)) { # Do various array2ish things with $item, some of which will _possibly_: push @array, ... } else { warn $item; } }