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

AW: seeking golfing advice

Thread Previous
From:
=?utf-8?Q?Moritz=2C_Georg?=
Date:
May 16, 2012 07:48
Subject:
AW: seeking golfing advice
Message ID:
zarafa.4fb394b6.6235.4372444b6b79da7c@virtual-261.str.topalis
> btw here is an example :
> 
> the code, applied on (1, 2, 3, 4) would return (1, 3). Thanks

so you want to check every element for oddity of its value, not its index, right?

@list = grep{$_%2}@array;

cheers,
0--gg-

> 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


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