In trying to compress a list, I wrote the following code:
my $compress = do {
my $previous;
$compress = -> $x {
if !defined $previous or $x ne $previous {
$previous = $x;
$x;
}
else {
();
}
};
}
my @compressed = map $compress, <a a a a b c c a a d e e e e>;
@compressed.say;
That's a bit more verbose than I intended, but that's partially because
of this:
my $compress = do {
my $previous;
$compress = -> $x {
if !defined $previous or $x ne $previous {
$previous = $x;
return $x;
}
else {
return ();
}
};
};
As soon as I put the return statements in an anonymous subroutine, I
get this error message:
*** Cannot use this control structure outside a 'routine' structure
at 99.pugs line 103, column 13-22
Take out the returns, and it works fine. Can someone tell me what I'm
missing? Is this a bug.
Also, I noticed I accidentally left off the final semi-colon on the do
block. It ran anyway and that surprised me.
Cleaner ways of writing that code are welcome, too.
Cheers,
Ovid
--
Buy the book -- http://www.oreilly.com/catalog/perlhks/
Perl and CGI -- http://users.easystreet.com/ovid/cgi_course/