Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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

by haukex (Prior)
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 in.txt >out.txt real 0m1.869s user 0m1.863s sys 0m0.004s $ time perl 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1190602]
Discipulus here..
[marto]: "what is normal or expected in any given circumstances."
[marto]: better: "If a type of behaviour, event, or situation is par for the course, it is not good but it is normal or as you would expect"
[Discipulus]: thanks i was unable to decide where to split the sentece
[marto]: FWIW search.cpan rarely has issues, see http://noc.perl. org for a route to report problems
[oakbox]: thanks, marto.
[marto]: there was a period where search.cpan had some frequent outages for (IIRC) a couple of weeks. I've not had any problems since, until today.
[marto]: this was about 16 months ago maybe. the noc team are, in my experience, very responsive to reports of issues, so please raise the issue after checking known problems/outages

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2017-07-26 10:16 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (388 votes). Check out past polls.