Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: printing every 2nd entry in a list backwards

by 1nickt (Prior)
on May 19, 2017 at 12:43 UTC ( #1190606=note: print w/replies, xml ) Need Help??


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

Another way:

my @x = (1, 22, 3, -4 ); print join " ", grep { state $i; $i++ % 2 == 0 } reverse @x;

The documentation advises against using map purely for its side effects. If you're not going to use the resulting list, use a for loop instead, or in the case of simply filtering a list down to a smaller list, grep.

As far as performant, I don't think you can tell much about the performance of the loop when you are opening and closing a file and only processing one line with four elements: the I/O will eat up most of the time no matter the solution.

Hope this helps!

update: show with grep


The way forward always starts with a minimal test.

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

    You are indeed correct with your observations about performance. However this can be easily mended by creating a file with 1_000_000 entries, each line having 10 numbers and then running the script against it

    Doing this on my machine, the despicable snake language comes out ahead

    $ time perl list.pl in >/dev/null real 0m2.150s user 0m2.136s sys 0m0.004s $ time python list.py in >/dev/null real 0m1.426s user 0m1.420s sys 0m0.000s

    I am looking to the monks for help on this one

      Doing this on my machine, the despicable snake language comes out ahead

      Even a snake can do what other beasts can't. No need to worry. Perl doesn't have a stepping range operator, it doesn't even have a descending one. So the indices must be built every line through. OTOH, knowing that the input lines always contain 10 items, it is trivial to beat the snake:

      qwurx [shmem] ~> time perl -lne 'BEGIN{$,=" "}print+(split)[9,7,5,3,1] +' in.txt >/dev/null real 0m1.589s user 0m1.588s sys 0m0.004s qwurx [shmem] ~> time python list.py in.txt >/dev/null real 0m2.204s user 0m2.188s sys 0m0.016s

      And your snake code does - according to the spec Given a line of numbers from a file, print every 2nd number starting from the back - get it right only for an even number of integers:

      qwurx [shmem] ~> python Python 2.7.9 (default, Jun 29 2016, 13:08:31) [GCC 4.9.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> line = "1 2 3 4" >>> line.split()[::-2] ['4', '2'] >>> line = "1 2 3 4 5" >>> line.split()[::-2] ['5', '3', '1'] >>> ^D

      So, what does every 2nd number starting from the back mean? If we start from the back and take every 2nd, the output for the even example should be ['3','1'], and for the odd example ['4','2']. If we count every 2nd from the beginning, the output ought to be ['4','2'] for both cases.

      Because you presented

      Example:

      1 22 3 -4 ==> -4 22

      almost all code examples in this thread assumed that every 2nd meant counting from the beginning, but outputting in reverse order. Tell the snake to do that, and compare again.

      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1190606]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2017-10-23 23:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (285 votes). Check out past polls.

    Notices?