From: "Ara.T.Howard" Date: 2005-07-18T22:00:39+09:00 Subject: Re: Reading total lines of file then displaying in reverse? On Mon, 18 Jul 2005, Tristan Knowles wrote: > How would I use File or IO to read all the lines of a > file, then display the contents in reverse? > > Its for a simple log reading script, which I would > like to have read the total lines, then display with > the latest entries first, and show x amount of log > entries per page. > > I'm currently just dumping the whole, or part of the > log with: > > ====== > IO.readlines("my.log")[1..49].each_with_index do > |line, index| > ====== > > I guess I would have to count all lines, then assign a > variable to the last number it counts, and then work > in reverse from this variable... hmmm. I'm not > sure:P > > Any ideas? if i understand you correctly: harp:~ > cat a.txt 42 1 2 3 4 5 6 7 8 9 10 harp:~ > cat a.rb lines = IO::readlines(ARGV.shift) page_separator = '=' * 79 page_size = 5 length = $. n_pages = (length / page_size) + 1 catch('done') do n_pages.times do page_size.times do throw 'done' if lines.empty? print lines.pop end puts page_separator end end harp:~ > ruby a.rb a.txt 10 9 8 7 6 =============================================================================== 5 4 3 2 1 =============================================================================== 42 '$.' is always the number of lines read after an IO::readlines. hth. -a -- =============================================================================== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | My religion is very simple. My religion is kindness. | --Tenzin Gyatso ===============================================================================