in reply to reduce like iterators
This is fairly easy to implement and wrap in a sub, even with the undefs. Once the sub is written you are back to having a one-liner. What would you like that this wouldn't do?
use strict; use warnings; sub compress { my $x; map { if (!defined($x)) { defined($_) ? ($x = $_) : () } else { defined($_) && ($x eq $_) ? () : ($x = $_) } } @_; } my @aData=(qw(a a a a b c c a a d e e e e), undef, undef, qw(f g g)); my @aCompressed = compress @aData; print "compressed: @aCompressed\n"; # outputs a b c a d e undef f g
Or if you want to play golf (though others I'm sure can do better)
sub compress { my$x;map{defined($x)?(defined($_)&&($x eq$_)?():($x=$_) +):defined($_)?($x=$_):()}@_; }
Update: added golf
Update:: fixed mistake - undefs were strings.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: reduce like iterators
by LanX (Saint) on Jan 03, 2011 at 18:50 UTC | |
by ikegami (Patriarch) on Jan 03, 2011 at 19:54 UTC | |
by LanX (Saint) on Jan 03, 2011 at 22:08 UTC | |
by ikegami (Patriarch) on Jan 03, 2011 at 23:17 UTC | |
by ELISHEVA (Prior) on Jan 03, 2011 at 20:04 UTC | |
by LanX (Saint) on Jan 03, 2011 at 22:24 UTC | |
by ELISHEVA (Prior) on Jan 03, 2011 at 23:08 UTC | |
by LanX (Saint) on Jan 04, 2011 at 19:49 UTC |