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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|