Good catch on the quotes around the filename. I intentionally left it all uncommented. But lets have some more fun with it:
#!/usr/bin/perl -w
use strict;
use IO::File;
use constant FILENAME => 'filename';
{
package cntr;
sub TIESCALAR{bless[pop],__PACKAGE__}
sub FETCH{ $_[0][0]++ }
}
my $file = IO::File->new( "< ". FILENAME )
or die "Failed to open '@{[FILENAME]}', $!";
my @elements;
{
local ( $a, $/ ) = $/.$/;
@elements = split $a => <$file>;
}
$file->close();
print "Here are the elements of the array:\n";
tie my $i, 'cntr', 1;
print map {$i.': {'."$_}$/"} @elements;
Yeah, that would go over well with a teacher or professor.
Update: So I changed the code a little from the original post. I actually tested it, and didn't like the way the newlines hung around. This fixes that. View source for the old code.
-
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.
|