Re: Get even postion elements in an array
by choroba (Cardinal) on Nov 16, 2010 at 12:20 UTC
|
Not much readable, but... @input[map 1+2*$_,0 .. @input/2]
| [reply] [d/l] |
Re: Get even postion elements in an array
by LanX (Saint) on Nov 16, 2010 at 12:27 UTC
|
| [reply] [d/l] [select] |
|
@output = @input[ grep $_ & 1, 0 .. $#input];
($mod, $val) = (2, 1);
@output = @input[ grep $_ % $mod == $val, 0 .. $#input];
| [reply] [d/l] [select] |
|
Why not be LESS cryptic, e.g.:-
my $flipflop = 1;
my @result = grep { $flipflop = not $flipflop; }, @input;
| [reply] [d/l] |
|
I prefer reusable idioms.
Here not is indeed more readable, but it won't produce 0 for false, which could be counterintuitive in other cases.
$flipflop needs to be declared, while $a (and $b) is already global as part of the sort idiom, which is a similar case of using a code block.
$a=1;
my @result = grep { $a = not $a }, @input;
At the end it's a matter of taste.
^= is shorter, = not is clearer, but =1- can equally be used in most other languages, making it an universally understood idiom.
| [reply] [d/l] [select] |
|
|
for completeness, an application of the new state feature for implicit initialization and scoping:
DB<23> use feature "state"; print grep { state $ff ^= 1 } 0..9
02468
DB<24> use feature "state"; print grep { not state $ff ^= 1 } 0..9
13579
| [reply] [d/l] |
Re: Get even postion elements in an array
by Ratazong (Monsignor) on Nov 16, 2010 at 13:10 UTC
|
TIMTOWTDI: Here is a way using a c-like loop:
for (my$i=1; $i <= $#input; $i++) { push (@output, $input[$i++]) };
You teacher will be fascinated by your double-usage of $i++ ;-) ......don't do this in production-code! | [reply] [d/l] [select] |
|
for (my $i=1; $i<=$#input; $i+=2) { push @output, $input[$i] }
...roboticus
| [reply] [d/l] [select] |
Re: Get even postion elements in an array
by dsheroh (Monsignor) on Nov 16, 2010 at 13:04 UTC
|
It feels like a horrible abuse of Perl's data types... and it emits a warning if @input has an odd number of elements... and you lose the original ordering of your elements... and you lose data entirely if two odd elements are the same... but you could do it by coercing @input into a temporary hash if you really wanted to:
{ my %foo = @input; @output = grep { defined } values(%foo); }
But I really, really doubt that you actually want to do it that way. | [reply] [d/l] [select] |
Re: Get even postion elements in an array
by JavaFan (Canon) on Nov 16, 2010 at 14:51 UTC
|
I'd use a simple grep, with a flipflop.
@output = do {my $i = 0; grep {$i++ % 2} @input};
| [reply] [d/l] |
Re: Get even postion elements in an array
by pKai (Priest) on Nov 16, 2010 at 16:16 UTC
|
@output = grep $|--, @input;
using some magic of $| for the flipflop.
But unless you use it for a real oneliner, use some less obscure formulation of the principal shown in previous replies. ;-) | [reply] [d/l] [select] |
|
$ perl -e 'my @a=(2, 3, 4, 5, 1, 7); print ("==> ", (map{$_ % 2 ? ():
+" $a[$_]"} 0..$#a), "\n");'
==> 2 4 1
| [reply] [d/l] |
Re: Get even postion elements in an array
by ww (Archbishop) on Nov 16, 2010 at 13:54 UTC
|
For the sake of your future usage (and maybe your grade for this class?), you may wish to note that "b" and "d" are elements "1" and "3" in @input; not the "even position elements." | [reply] [d/l] |
|
| [reply] |