in reply to Re^3: reduce like iterators
in thread reduce like iterators
Shoot! I totally get the semi-predicate problem, and you stated the problem clearly in the OP, but I thought I had a brainwave and rewrote my UPDATED code, which had actually accounted for the problem.
So anyway, I re-read the entire thread and I sort of get why you'd want, say, a $^PRE special variable. But it is so trivial, I don't see why you'd bother. You are simply dealing with a list in pairwise fashion, using a previous value to evaluate a current value. Eliminating adjacent dupes is trivial...
except for an initial undefined value, but only because $p is initially undefined. So you define it and it works. And it's easy to remember.@new = map { $p ne $_ ? $p = $_ : () } @orig;
You could define a uniq_adj function...$p = 'supercalifragilistic'; @new = map { $p ne $_ ? $p = $_ : () } @orig;
use strict; sub uniq_adj { my $p = 'supercalifragilistic'; return map { $p ne $_ ? $p = $_ : () } @_; } my @orig = (undef, undef, qw(a a b b c 0 c d d u u 0 0 "0" "0" '0' + '0')); my @new = uniq_adj @orig; print "'", join ("' '", @new), "'\n"; __END__ Prints --> '' 'a' 'b' 'c' '0' 'c' 'd' 'u' '0' '"0"' ''0''
I printed with single quotes to show where undef's are being printed. Note that 0, "0", and '0' are preserved. It's very generalizable. I predefined $p, but isn't that less work than having Perl do it through a built-in? I read through the entire thread again and it seems to fit the bill. Is this kind of what you're looking for?
Regardless, cheers!
--marmot
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^5: reduce like iterators
by LanX (Saint) on Jan 19, 2011 at 00:09 UTC | |
by tilly (Archbishop) on Jan 19, 2011 at 01:14 UTC | |
by LanX (Saint) on Jan 19, 2011 at 12:53 UTC | |
by furry_marmot (Pilgrim) on Jan 19, 2011 at 01:59 UTC |