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.


In reply to Printing the last few elements of an array. by jonnyfolk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.