my @tmp; while (my $elem = pop @array) { # or s/pop/shift/ # do stuff if ($keep) { push @tmp, $elem; } } @array = @tmp;