Oh wise oh, powerful monks!
On p.97 of Llama, merlyn states thusly:
Write a program that acts like cat, but reverses the order of the output lines. (Some systems have a utility like this called tac.) If you run yours as ./tac fred barney betty, the output should be all of file betty from last line to first, then barney then fred, also from last line to first.I seem to be having trouble reversing the input, but not the files.
#!/usr/bin/perl -w
use strict;
@ARGV = reverse @ARGV;
while (<>) {
chomp;
print "\n@_\n";
}
I've tried a couple of different approaches that didn't work. Here's an example:
#!/usr/bin/perl -w
my $in;
@ARGV = ( reverse @ARGV);
foreach my $x (@ARGV){
@in = <$x>;
@in = reverse @in;
}
while (@in) {
chomp;
print "\n$_\n";
}
I get perl complaining of
"readline() on unopened file at ./tac.pl line 7."when I tried that solution. This seems simple, but in practice I am stumped. I'd appreciate the help of a more powerful monk.
Many THANKS!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.