You might want to be using the <> operator here. <> reads
from the files passed in ARGV (if they exist) or from STDIN
otherwise. This means that you can have this behaviour with
no extra effort if you want it in the future.
It'll certainly save you from having to worry about opening
each of those files and the like. So your code could
simplify to:
#!/usr/bin/perl -w
use strict;
use diagnostics;
if(-e "out.txt") {
# complain about file.
}
my @out;
while(<>) {
chomp;
push @out, lc($_);
}
# do stuff with @out:
@out = sort @out;
# print it (to STDOUT for the moment)
print join("\n", @out);
I agree with everyone else here, that you probably don't
want to be inserting the newline until you're ready to
output the data. If you decide, for some reason, later to
reverse all the characters on each of your lines or some such,
that newline character can get in the way.
Good luck.
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.