Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Get even postion elements in an array

by Anonymous Monk
on Nov 16, 2010 at 12:13 UTC ( [id://871696]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

I want get even position elements in an array with single line statement

Example: @input=('a','b','c','d','e'); @output;

Output will be

@output contains b and d

Replies are listed 'Best First'.
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]
Re: Get even postion elements in an array
by LanX (Saint) on Nov 16, 2010 at 12:27 UTC
    $a=1;print grep {$a=1-$a} @input

    or, more cryptic

    $a=1;print grep {$a^=1} @input

    Cheers Rolf

      @output = @input[ grep $_ & 1, 0 .. $#input];
      ($mod, $val) = (2, 1); @output = @input[ grep $_ % $mod == $val, 0 .. $#input];
      Why not be LESS cryptic, e.g.:-
      my $flipflop = 1; my @result = grep { $flipflop = not $flipflop; }, @input;

      One world, one people

        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.

        Cheers Rolf

      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

      Cheers Rolf

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!

      Ratazong:

      If you're going to use a C-style loop, why use the extra $i++?

      for (my $i=1; $i<=$#input; $i+=2) { push @output, $input[$i] }

      ...roboticus

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.
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};
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. ;-)

      $ perl -e 'my @a=(2, 3, 4, 5, 1, 7); print ("==> ", (map{$_ % 2 ? (): +" $a[$_]"} 0..$#a), "\n");' ==> 2 4 1
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."
      Objection. The "first position" is element 0, but 1 is odd. =)

      But yes, I agree, it could be phrased more clearly.

      Cheers Rolf

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://871696]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-24 12:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found