my $replace = 0; for my $x (0 .. $#array) { next unless # pass of your condition; $array[$replace++] = $array[$x]; } #### your condition and $array[$replace++] = $array[$_] for 0 .. $#array;