Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: printing every 2nd entry in a list backwards

by KurtZ (Pilgrim)
on May 19, 2017 at 12:38 UTC ( #1190605=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1190605]
help
Chatterbox?
[soonix]: can confirm: in the comment form, "preview" activates "create" button, in the new post form it doesn't.

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2017-12-15 06:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (418 votes). Check out past polls.

    Notices?