Every other

Dirk Koopman djk at tobit.co.uk
Fri Oct 30 18:01:23 GMT 2009


Richard Huxton wrote:
> Philip Potter wrote:
>> 2009/10/30 Mark Fowler <mark at twoshortplanks.com>:
>>> my $i;
>>> my @new = grep { $i = !$i } @old;
>> Thinking about it further, this could be more readable as:
>>
>> my @new = grep { ++$i % 2 } @old;
> 
> This one gets my vote for "the only one I'd want to come across without
> a comment above it".
> 

Not certain why everyone avoids map and uses grep, but here's my pennyworth:

@a = (0..20);
@b = map {($i ^= 1) ? () : $_} @a;
print join(',', @b), "\n";

gives

1,3,5,7,9,11,13,15,17,19

Obviously if you reverse the () and the $_, you get:

0,2,4,6,8,10,12,14,16,18,20

Dirk


More information about the london.pm mailing list