develooper Front page | perl.fwp | Postings from May 2012

Re: seeking golfing advice

Thread Previous | Thread Next
From:
Peter Makholm
Date:
May 16, 2012 04:31
Subject:
Re: seeking golfing advice
Message ID:
87k40c1hy4.fsf@vps1.hacking.dk
damien krotkine <dkrotkine@gmail.com> writes:

> I'm using this code to get a list of only the odd elements of an
> array. The resulting list must have the same order as the array.
>
> map { state $f; ($_) x (++$f%2) }  @array;

If you want only to get some elements of a list is is much more obvious
to use grep instead of map:

  grep { ++$f%2 } @array

Then use the magical flip-flop variable:
 
  grep { $|-- } @array

And remove unneeded syntax

  grep$|--,@array

//Makholm


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About