Re: seeking golfing advice

2012-05-19 Thread Smylers
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 indi

Re: seeking golfing advice

2012-05-18 Thread John Douglas Porter
> From: Aristotle Pagaltzis > Subject: Re: seeking golfing advice > To: fwp@perl.org > Date: Friday, May 18, 2012, 5:29 AM > * Steve Fink > [2012-05-18 10:25]: > > On Thu, May 17, 2012 at 3:14 AM, Aristotle Pagaltzis > > wrote: > > > * Mike Erickson >

Re: seeking golfing advice

2012-05-18 Thread Aristotle Pagaltzis
* Steve Fink [2012-05-18 10:25]: > On Thu, May 17, 2012 at 3:14 AM, Aristotle Pagaltzis wrote: > > * Mike Erickson [2012-05-16 15:45]: > > > If you don't care about order, but just want those elements, you > > > can also do: > > > > > > keys%{{@a}} > > > > There is more than order that gets lost

Re: seeking golfing advice

2012-05-18 Thread Steve Fink
On Thu, May 17, 2012 at 3:14 AM, Aristotle Pagaltzis wrote: > * Mike Erickson [2012-05-16 15:45]: >> If you don't care about order, but just want those elements, you can >> also do: >> >> keys%{{@a}} > > There is more than order that gets lost. If you use `keys` you also get > everything back str

Re: seeking golfing advice

2012-05-17 Thread Aristotle Pagaltzis
* 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

Re: seeking golfing advice

2012-05-17 Thread Aristotle Pagaltzis
* Mike Erickson [2012-05-16 15:45]: > If you don't care about order, but just want those elements, you can > also do: > > keys%{{@a}} There is more than order that gets lost. If you use `keys` you also get everything back stringified – undefs are lost and references break. If you use `values` the

Re: seeking golfing advice

2012-05-16 Thread Ronald J Kimball
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 eleme

AW: seeking golfing advice

2012-05-16 Thread Moritz , Georg
> 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 wrote: > > Hi, > > > > I'm using thi

Re: seeking golfing advice

2012-05-16 Thread Mike Erickson
On 05/16/2012 07:46 AM, damien krotkine wrote: Hi all, thanks for the quick answers :) first of all, despite my attempt to clear things up with an example, I failed :) What I wante is "every other element, beginning with the first", so : ( 'foo', 3, 42, 'bar) should become ( 'foo', 42). If

Re: seeking golfing advice

2012-05-16 Thread Pau Amma
On Wed, May 16, 2012 12:25 pm, damien krotkine wrote: > On 16 May 2012 14:02, Peter Makholm wrote: > [ ... ] > >> So basically you are trading 8 characters for readability and probably >> speed. Why do you want that? > > For fun, purely. I'll stick with $f % 2 for now, it seems a right > balance c

Re: seeking golfing advice

2012-05-16 Thread damien krotkine
On 16 May 2012 14:02, Peter Makholm wrote: > damien krotkine writes: > >> Indeed grep is much better. As the code is used in a more complex >> structured I got lost and confused and ended up using map, blah. >> >> $|-- was what I was looking for :) > > No, reading my posts again it I see that it

Re: seeking golfing advice

2012-05-16 Thread Peter Makholm
damien krotkine writes: > Indeed grep is much better. As the code is used in a more complex > structured I got lost and confused and ended up using map, blah. > > $|-- was what I was looking for :) No, reading my posts again it I see that it clearly doesn't do what you ask for. You are actually

Re: seeking golfing advice

2012-05-16 Thread damien krotkine
Hi all, thanks for the quick answers :) first of all, despite my attempt to clear things up with an example, I failed :) What I wante is "every other element, beginning with the first", so : ( 'foo', 3, 42, 'bar) should become ( 'foo', 42). Indeed grep is much better. As the code is used in a

Re: seeking golfing advice

2012-05-16 Thread John Douglas Porter
And of course, use grep, as others have said. @list[ grep !$_%2, 0..$#list ]; that gets you every other element, beginning with the first. --- On Wed, 5/16/12, John Douglas Porter wrote: > From: John Douglas Porter > Subject: Re: seeking golfing advice > To: fwp@perl.org > Dat

Re: seeking golfing advice

2012-05-16 Thread Peter Makholm
Peter Makholm writes: > damien krotkine writes: > >> 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; Wow, you asked for golfing advice in the subject and a nice

Re: seeking golfing advice

2012-05-16 Thread John Douglas Porter
On Wed, 5/16/12, damien krotkine wrote: > From: damien krotkine > Subject: Re: seeking golfing advice > To: fwp@perl.org > Date: Wednesday, May 16, 2012, 7:19 AM > btw here is an example : > > the code, applied on (1, 2, 3, 4) would return (1, 3). > Thanks > > On 1

Re: seeking golfing advice

2012-05-16 Thread Peter Makholm
damien krotkine writes: > 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; If you want only to get some elements of a list is is much more obvious to use grep instead

Re: seeking golfing advice

2012-05-16 Thread Daniel Cutter
On 16.05.2012 13:19, damien krotkine wrote: btw here is an example : the code, applied on (1, 2, 3, 4) would return (1, 3). Thanks On 16 May 2012 13:15, damien krotkine 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 or

Re: seeking golfing advice

2012-05-16 Thread damien krotkine
btw here is an example : the code, applied on (1, 2, 3, 4) would return (1, 3). Thanks On 16 May 2012 13:15, damien krotkine 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; ($

seeking golfing advice

2012-05-16 Thread damien krotkine
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