John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:
For hashes, the each iterator expressly allows you to delete that element without messing things up, and you have the key so it's possible.
For an array, the "key" is an integer, and that's not provided in the foreach.
grep returns a list and means something else in scalar context, but I can live with that, just add extra parens on the left side. Easy to mess up as an idiom, though.
I can maintain my own counter in the block of a foreach, use a C-style index loop, or move all the items from one list to another (I prefer to mod in-place since it's a reference parameter).
I certainly can do it. But I wonder if there is a better way to do it that I can add to my bag of tricks.
Thanks,
—John
|
|---|