jonnyfolk has asked for the wisdom of the Perl Monks concerning the following question:
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:
($record1 is then used in conjunction with HTML::Template.)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;
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: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.@record = reverse @record; my @record1; my $count = 0; while (($count < 5) && (my $line = @record)) { push (@record1, $line); $count += 1; } print "@record1";
Thanks very much.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Printing the last few elements of an array.
by rob_au (Abbot) on Dec 16, 2002 at 11:33 UTC | |
by jonnyfolk (Vicar) on Dec 16, 2002 at 13:31 UTC | |
Re: Printing the last few elements of an array.
by thinker (Parson) on Dec 16, 2002 at 11:40 UTC | |
by jonnyfolk (Vicar) on Dec 16, 2002 at 14:31 UTC | |
Re: Printing the last few elements of an array.
by Molt (Chaplain) on Dec 16, 2002 at 14:59 UTC | |
Re: Printing the last few elements of an array.
by dakkar (Hermit) on Dec 16, 2002 at 17:29 UTC | |
by jonnyfolk (Vicar) on Dec 16, 2002 at 19:42 UTC | |
by dakkar (Hermit) on Dec 19, 2002 at 09:07 UTC | |
by jonnyfolk (Vicar) on Dec 20, 2002 at 00:02 UTC | |
Re: Printing the last few elements of an array.
by tachyon (Chancellor) on Dec 17, 2002 at 10:58 UTC |
Back to
Seekers of Perl Wisdom