"be consistent" | |
PerlMonks |
Re: reduce like iteratorsby furry_marmot (Pilgrim) |
on Jan 04, 2011 at 01:16 UTC ( [id://880281]=note: print w/replies, xml ) | Need Help?? |
Hi LanX, map{} will pass on undef's, but grep{} needs undef to tell whether something passed the filter. But you can stack any number of maps and greps together, which is how I came up with a workaround for it, using two maps and a grep. The first map{}, reading from the bottom up, simply marks duplicates in-a-row (as opposed to any dupes at all) by changing them to 'aardvark'. Undef's and zeroes go through without problem. The second map{} changes actual undefined elements to the text 'undef'. Change these as suits your algorithm. The grep{} is where the duplicates/aardvarks are finally removed. It seems to work, accommodating undefined values, while still being simple enough to use in a one-liner, and uses array refs so it doesn't pass whole lists back and forth. And the one-liner -- actually broken up for easier viewing:
I hope you find this interesting/useful. --marmotUPDATE: I was thinking about this some more, and realized I had made it waaaay too complicated. The nodupes() below pretty much is a one-liner, and accomodates 0, "0", and undef just fine. And nodupes() can be used inline with other maps and greps. In the end, the grep was the only thing needed.
In Section
Seekers of Perl Wisdom
|
|