in reply to Printing Stacks
Another way: Print last n lines of array in reverse order:
The @stack[ -$n .. -1 ] expression is an array slice. (Update: Negative indices index from the end of the array.)c:\@Work\Perl\monks>perl -wMstrict -e "my @stack = ( 'line 1', 'line two', 'third line', 'line the fourth', 'almost there', 'last line', ); ;; my $n = 3; ;; print qq{$_ \n} for @stack[ -$n .. -1 ]; " line the fourth almost there last line
Update: Oops... Just realized the lines are not printed in reverse order. Oh well, easily fixed (but not as neat):
c:\@Work\Perl\monks>perl -wMstrict -e "my @stack = ( 'line 1', 'line two', 'third line', 'third from last', 'almost there', 'last line', ); ;; my $n = 3; ;; print qq{$_ \n} for reverse @stack[ -$n .. -1 ]; " last line almost there third from last
Give a man a fish: <%-{-{-{-<
|
|---|