in reply to How do I remove an element from any array while iterating over it in a loop?

If you iterate through the array backwards you can avoid the need to adjust for array elements changing indicies, as you would have to iterating through it forwards:

# ... for ($i = $#arry; $i >= 0; $i--) { next unless $arry[$i] == 5; splice @arry, $i, 1; # no need to adjust $i here, going backwards. }

However, grep is probably better for this task.

  • Comment on Re: How do I remove an element from any array while iterating over it in a loop?
  • Download Code