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

Re: seeking golfing advice

Thread Previous | Thread Next
Aristotle Pagaltzis
May 17, 2012 03:28
Re: seeking golfing advice
Message ID:
* Pau Amma <> [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}
#Aristotle Pagaltzis // <>

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About