Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: printing every 2nd entry in a list backwards (updated)

by haukex (Abbot)
on May 19, 2017 at 12:13 UTC ( #1190602=note: print w/replies, xml ) Need Help??


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

$ cat in.txt 1 22 3 -4 a b c d e f g h i j k l m n o $ perl -anle 'print "@F[map $#F-$_*2, 0..$#F/2]"' in.txt -4 22 a c f d j h o m k

But seriously, there are lots of ways to do this. Just one of many:

while (<>) { my @fields = split; for (my $i=$#fields; $i>=0; $i-=2) { print "$fields[$i] "; } print "\n"; }

Update: As for your code, I'd probably have used for instead of map, but other than that it's a decent solution. Just for fun, a different way to write that might be: ++$i&1 and print "$_ " for reverse split; although that might be getting a little too clever ;-) This is a fun exercise in TIMTOWTDI! Update 2: Changed wording a bit.

Replies are listed 'Best First'.
Re^2: printing every 2nd entry in a list backwards
by Anonymous Monk on May 19, 2017 at 12:25 UTC

    Thank you kindly for your reply.

    Your first solution is indeed exuding the knowledge of a monk, sadly it is rather slow.

    Your second solution is actually what I tried first but it is also slower than my current solution. I am explicitly looking for a fast solution to beat the solution written in the language that also starts with "P" and must not be named.

    I apologize for not being clear in the first place.

      Your second solution is actually what I tried first but it is also slower than my current solution.

      How exactly did you measure this - code, sample input, etc.? Because at least under the following conditions, my code suggestion appears to be faster than yours. (Note I inserted use warnings; use strict; at the top of my script.)

      $ perl -wMstrict -le 'print join(" ",map {int(rand(100))-50} 0..rand(7 +)+3) for 1..1000000' >in.txt $ wc -l in.txt 1000000 in.txt $ head -5 in.txt -49 43 0 -35 0 -20 -49 5 46 -11 -14 39 39 -24 -49 36 -7 -36 -43 15 30 5 -4 11 37 -25 27 -49 21 49 33 -15 -16 17 10 32 -14 -30 $ time perl 1190600.pl in.txt >out.txt real 0m1.869s user 0m1.863s sys 0m0.004s $ time perl 1190602.pl in.txt >out.txt real 0m1.539s user 0m1.521s sys 0m0.016s

        Apologies for not mentioning how I measure performance, please refer to my other reply for more information

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1190602]
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 taking refuge in the Monastery: (3)
As of 2017-12-15 06:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (418 votes). Check out past polls.

    Notices?