in reply to (MeowChow) Re2: How do I remove an element from any array while iterating over it in a loop?
in thread How do I remove an element from any array while iterating over it in a loop?

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?

  • Comment on Re: (MeowChow) Re2: How do I remove an element from any array while iterating over it in a loop?

Replies are listed 'Best First'.
Re (tilly) 4: How do I remove an element from any array while iterating over it in a loop?
by tilly (Archbishop) on Aug 31, 2001 at 00:53 UTC
    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.

      I guess I didn't really consider returning an empty list a "trick" with map. I know I can return multiple elements, that whatever I return is raveled up into the output list, and zero is just a case of that.

      Here's the summary:

      @array= map { map_proc } @array; @array= grep { map_proc } @array;
      where grep_proc returns true (keep) or false (discard); and map_proc returns the original element to keep, or an empty list to discard.

      I agree that grep gives the idea of filtering. But he's not just filtering; he's altering. Altering where "delete" is one possibility.

      —John

        I am perfectly aware of the fact that it is just a special case, the summary was not needed. However I am also aware from experience that people tend to look at you strangely when you have different numbers of return values in different cases. And furthermore the request was explicitly not a case of modifying elements, it was a straight filtering operation. Given that here are both strategies side by side if you assume that $unwanted is the element you are trying to remove:
        @filtered = map {$_ eq $unwanted ? () : $_} @original; @filtered = grep {$_ ne $unwanted} @original;
        Most people find the second much more readable. If you don't see why, then I suggest grabbing some co-workers, showing and explaining both, and trying to explain to them why they should find map more readable...

        Remember, generality often conflicts with readability. map is a far more general and complex function than grep. As such, when you see it you need to read very carefully, because a small detail can make a huge difference. But straight filtering is a very common need, and in that common case grep is simpler to write and easier to read.