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

Re: seeking golfing advice

Thread Previous | Thread Next
From:
Daniel Cutter
Date:
May 16, 2012 04:29
Subject:
Re: seeking golfing advice
Message ID:
4FB38F91.2060301@agac.de
On 16.05.2012 13:19, damien krotkine wrote:
> btw here is an example :
>
> the code, applied on (1, 2, 3, 4) would return (1, 3). Thanks
>
> On 16 May 2012 13:15, damien krotkine<dkrotkine@gmail.com>  wrote:
>> Hi,
>>
>> 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;
>>
>> I'm looking for advice to make it shorter or nicer. Everything in perl
>> 5.12 is allowed, but must pass use strict. I've failed at using the
>> '..' operator to act as a flip/flop operator...
>>
>> thanks,
>> dams

Isn't this what grep is for?

   1 #!perl
   2 use strict;
   3 my @array=(1,2,3,4);
   4 my $f;
   5 my @result = grep {++$f%2} @array;
   6 print "@result\n";

-- 
*Daniel Cutter*

s/\b[^a]/\u$&/g,s;$;,;,print for join$,,map{('acehjklnoprstu'
=~m(.)g,$")[/\d/?$_:hex]}q(4dbce078c32ae92a6e30152aff)=~m(.)g


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