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

Re: seeking golfing advice

From:
Ronald J Kimball
Date:
May 16, 2012 08:22
Subject:
Re: seeking golfing advice
Message ID:
20120516152227.GA59858@penkwe.pair.com
On Wed, May 16, 2012 at 04:40:40AM -0700, John Douglas Porter wrote:
> And of course, use grep, as others have said.
> 
> @list[ grep !$_%2, 0..$#list ];
> 
> that gets you every other element, beginning with the first.

! has higher precedence than %, so this actually gets you just the
first element.


You need to add parentheses to get the correct result:

@list[grep!($_%2),0..$#list];


There are a couple ways to shave a character from that:

@list[grep$_%2-1,0..$#list];
@list[grep$_+1&1,0..$#list];

Ronald



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