http://www.perlmonks.org?node_id=1190605


in reply to printing every 2nd entry in a list backwards

Print is slow, you can try to speed up your solution by taking it out of the loop
print map {$i++ & 1 ? "" : "$_ "} reverse split(/ /);
Also a common trick for a flip flop boolean state is inversion with xor $i^=1
In hindsight you want grep not a map
$,=" "; print grep { $i^=1 } reverse split(/ /);

updated $,=" " , haukex++

Replies are listed 'Best First'.
Re^2: printing every 2nd entry in a list backwards
by Anonymous Monk on May 19, 2017 at 14:17 UTC
    Don't forget about
    $, = " "
    to make sure the output is properly formatted.
Re^2: printing every 2nd entry in a list backwards
by Anonymous Monk on May 19, 2017 at 14:01 UTC

    The one with grep is amazingly fast, kudos