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

Use map. @array= map { dostuff } @array; The block of code ("dostuff" here) is called once for each element with $_ aliased with that element. It returns $_ unaltered or returns an empty list to delete that element.

Replies are listed 'Best First'.
(MeowChow) Re2: How do I remove an element from any array while iterating over it in a loop?
by MeowChow (Vicar) on Aug 30, 2001 at 23:00 UTC
      Hmm, using grep, your "test" is the iteration code and returns true to keep, false to discard. Cute. Maybe too cute to understand later, though?

        Using map when grep would do takes more code and an easy to miss trick with returning empty lists. Plus saying map gives no indication of why you have the operation. Saying grep gives a strong indication of filtering to anyone who has been near Unix or who has read perlfunc.

        Therefore I would say that using map and tricky return lists is significantly less maintainable than using grep where grep would fit.