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

Re: seeking golfing advice

Thread Previous
From:
Smylers
Date:
May 19, 2012 03:19
Subject:
Re: seeking golfing advice
Message ID:
20120519101917.GA1836@stripey.com
Aristotle Pagaltzis writes:

> And if you have an array to work with in the first place, you can also
> 
>     @array[map 1+$_*2, 0..$#array/2]
> 
> which is 3 characters longer than the grep version

I think that actually gives you the even elements. For the odd elements
(which are even array indices, in Perl's terms) you don't need the C<
1+> in there, saving 3 characters.

  @array[map$_*2,0..$#array/2]

The shortest variant like that I've found for even characters is:

  @array[map$_*2-1,1..@array/2]

@array is one character shorter than $#array, but to avoid getting an
unwanted undef element when @array has an even number of elements in it
you need to start at 1 rather than 0, which makes the subtraction
necessary.

Smylers
-- 
http://twitter.com/Smylers2

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