That begs a question of semantics. Do you mean the even vs odd values?

Then the common idiom would be something like

my (@even, @odd); push @{ $_ % 2 ? \@odd : \@even }, $_ for split /,/, $text;

Here, you loop over all the elements, giving push either @odd or @even to push the element onto, depending on whether the element is odd or even. The awkward @{ ... } construct is necessary because push demands that its first argument have a @ sigil in front, no matter what.

Or, do you mean the even vs odd positions in the list?

Then you'd use something like

my (@even, @odd); my $i = 0; push @{ $i++ % 2 ? \@odd : \@even }, $_ for split /,/, $text;

Here, you increase a position counter at each iteration, and make the decision depending on its even/oddness.

Update: see Re^6: split every other value about the following code and Re^7: split every other value for my reply with a correct derivative.

Another option in this case is something like

my @field = split /,/, $text; my (@even, @odd) = @field[ map { $_, $_ + @field / 2 } 0 .. ( @field / 2 ) - 1 ];

Makeshifts last the longest.


In reply to Re: split every other value by Aristotle
in thread split every other value by jcpunk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.