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

Re: seeking golfing advice

Thread Previous | Thread Next
From:
John Douglas Porter
Date:
May 16, 2012 04:35
Subject:
Re: seeking golfing advice
Message ID:
1337168138.36905.YahooMailClassic@web161606.mail.bf1.yahoo.com
It's not clear whether you want "every other element, beginning with the first" or "every numeric element with the property 'odd'".  Your example doesn't clear that up at all. :-)

If it's the latter you want:

map { $_ & 1 ? $_ : () } @l;


--- On Wed, 5/16/12, damien krotkine <dkrotkine@gmail.com> wrote:

> From: damien krotkine <dkrotkine@gmail.com>
> Subject: Re: seeking golfing advice
> To: fwp@perl.org
> Date: Wednesday, May 16, 2012, 7:19 AM
> 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
>

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