Reading total lines of file then displaying in reverse?

T

Tristan Knowles

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:

=3D=3D=3D=3D=3D=3D
IO.readlines("my.log")[1..49].each_with_index do
|line, index|
=3D=3D=3D=3D=3D=3D

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?


=09
___________________________________________________________=20
How much free photo storage do you get? Store your holiday=20
snaps for FREE with Yahoo! Photos http://uk.photos.yahoo.com
 
S

Stefan Holst

Tristan said:
How would I use File or IO to read all the lines of a
file, then display the contents in reverse?

something like this?

File.readlines("my.log").reverse.each{|l| puts l}

RY
Stefan
 
T

Tristan Knowles

Cool. That reverses the value of the lines ordered,
but still lists the line numbers from 0 -> x.

Anyway to reverse this also, x -> 0?


Thanks again.

--- Stefan Holst said:
=20
something like this?
=20
File.readlines("my.log").reverse.each{|l| puts l}
=20
RY
Stefan
=20
=20
=20



=09
=09
=09
___________________________________________________________=20
Yahoo! Messenger - NEW crystal clear PC to PC calling worldwide with voic=
email http://uk.messenger.yahoo.com
 
D

daz

Stefan said:
something like this?

File.readlines("my.log").reverse.each{|l| puts l}

RY
Stefan

There's Array#reverse_each which would save an
unnecessary array reversal :)

File.readlines("my.log").reverse_each{|l| puts l}


daz
 
D

David A. Black

Hi --

something like this?

File.readlines("my.log").reverse.each{|l| puts l}

I believe that's identical to:

puts File.readlines("my.log").reverse


David
 
T

Tristan Knowles

--- "David A. Black said:
=20
I believe that's identical to:
=20
puts File.readlines("my.log").reverse


For my case, it now looks like this:

lines =3D IO.readlines("my.log").reverse.each_with_index
do |line, index|


I'm trying to use .length somewhere now to count the
number of lines.

Tristan


=09
=09
=09
___________________________________________________________=20
Yahoo! Messenger - NEW crystal clear PC to PC calling worldwide with voic=
email http://uk.messenger.yahoo.com
 
D

David A. Black

Hi --

For my case, it now looks like this:

lines = IO.readlines("my.log").reverse.each_with_index
do |line, index|


I'm trying to use .length somewhere now to count the
number of lines.

One possibility:

class Array
def map_with_index
res = []
each_with_index {|e,i| res << yield(e, i) }
res
end
end

puts File.readlines("my.log").map_with_index {|*a| a }.reverse

(Not optimally efficient, I think, but anyway.)


David
 
A

Ara.T.Howard

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
===============================================================================
 
B

Brian Candler

For my case, it now looks like this:

lines = IO.readlines("my.log").reverse.each_with_index
do |line, index|


I'm trying to use .length somewhere now to count the
number of lines.

lines.size will give you the number of lines. Are you trying to do something
like this?

lines = File.readlines("my.log")

lines.reverse.each_with_index do |line, index|
index = lines.size - index
printf "%5d %s", index, line
end

Or:

c = lines.size
lines.reverse_each do |line|
printf "%5d %s", c, line
c -= 1
end

Or the fancy way, if you want to iterate using each_with_index but not
create an intermediate reversed copy of the array:

require 'enumerator'
lines.to_enum:)reverse_each).each_with_index do |line, index|
index = lines.size - index
printf "%5d %s", index, line
end

Regards,

Brian.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top