Hallowed monks,
lowly Perl apprentice here with a question regarding list traversal.
I chanced upon this code written in a lesser language:
import sys f = open(sys.argv[1], "r") for line in f: for n in line.split()[::-2]: sys.stdout.write(n + " ") print("");
Being ever curious I wondered how this could be done in Perl.
Trying to wield what little power of Perl I possess, I came up with the following:
use strict; use warnings "all"; open(my $fh, "<", "$ARGV[0]") or die; while (<$fh>) { chomp; my $i = 0; map {print $i++ & 1 ? "" : "$_ "} reverse split(/ /); print "\n"; } close $fh;
The task seems simple at a glance:
Given a line of numbers from a file, print every 2nd number starting from the back.
Example:
1 22 3 -4 ==> -4 22
Are the esteemed monks able to enlighten me with a solution that is better and more performant?
Faithfully yours,
Perl apprenticeIn reply to printing every 2nd entry in a list backwards by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |