your condition and $array[$replace++] = $array[$_] for 0 .. $#array;