I have an array and I would like to print the last few elements (most recent) to the screen. I have put together the following code to make this happen:
open (FILE, "$record") or die 'can\'t open record page: $!';
my @record =<FILE>;
close FILE;
my @record = reverse @record;
my @record1 = ($record[0], $record[1], $record[2], $record[3], $re
+cord[4], $record[5], $record[6], $record[7], $record[8], $record[9]);
+
my $record1 = join "<br>", @record1;
($record1 is then used in conjunction with HTML::Template.)
This is obviously not very perl so I thought I'd get some input from anyone kind enough to help me as to the alternatives I might have used to do the same thing.
One of the things I did try was the following:
@record = reverse @record;
my @record1;
my $count = 0;
while (($count < 5) && (my $line = @record)) {
push (@record1, $line);
$count += 1;
}
print "@record1";
This produced some very odd results so it's obvious that just as in the "Hitchhiker's Guide" I don't understand the question I asked! If someone does and could explain, that would be great.
Thanks very much.
-
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.