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

Re: seeking golfing advice

From:
Aristotle Pagaltzis
Date:
May 17, 2012 03:28
Subject:
Re: seeking golfing advice
Message ID:
20120517102820.GD7364@fernweh.plasmasturm.org
* Pau Amma <pauamma@gundo.com> [2012-05-16 14:55]:
> If, as it sounds, you want to balance golfiness and strictness, you
> could also say:
>
> @array[grep $_%2, keys @array]
>
> (or @array[grep $_%2^1, keys @array] if you set $[ to 1 - but you
> didn't do that, right? :-) )

Btw, `keys@foo` and `0..$#foo` are equally long… but the latter works
with old perls (advantage production) *and* looks more line-noisy
(advantage golf).

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 in exchange for doing
half as much work. (For even elements, the map and grep solutions would
yield exactly equally long code, with half-as-much-work still applying.)

-- 
*AUTOLOAD=*_;sub _{s/$/$"/;s/(.*):://;wantarray//substr$_,-1,1,",$/";print;$1}
&Just->another->Perl->hack;
#Aristotle Pagaltzis // <http://plasmasturm.org/>



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