Navigating Files by Line Number

J

John Baker

Hi

I've tried the following code on Win XP, Ruby 1.8rc1 and
Mac OS X 10.3, Ruby 1.6.8. From the online documentation I
pretty much copied and tried the following code:

f = File.new("testfile.txt")
puts f.gets >> This is line 1
puts f.gets >> This is line 2
f.lineno = 9 >>
puts f.lineno >> 9
puts f.gets >> This is line 3

Although f.lineno updates to 9, it never seems to affect
the line number next read.

Thanks
John
______________________________________________________________
Herbalife Independent Distributor http://www.healthiest.co.za
 
J

Jason Wold

That seems to fit with the ri description of File.lineno=. It sets
lineno but doesn't seem to seek to that line in the file. I think
what you want could be achieved by:

f.rewind
1.upto(9) { f.readline }

or even extending the File class to add a function that does this

Class File
def seek_line(line)
self.rewind
1.upto(line) { self.readline }
end
end

Cheers,
Jason
 
G

George Ogata

John Baker said:
I've tried the following code on Win XP, Ruby 1.8rc1 and
Mac OS X 10.3, Ruby 1.6.8. From the online documentation I
pretty much copied and tried the following code:

f = File.new("testfile.txt")
puts f.gets >> This is line 1
puts f.gets >> This is line 2
f.lineno = 9 >>
puts f.lineno >> 9
puts f.gets >> This is line 3

Although f.lineno updates to 9, it never seems to affect
the line number next read.

IO#lineno= just sets the counter, not the position in the file. This
is demonstrated in the ri example:

------------------------------------------------------------- IO#lineno=
ios.lineno = integer => integer
------------------------------------------------------------------------
Manually sets the current line number to the given value. +$.+ is
updated only on the next read.

f = File.new("testfile")
f.gets #=> "This is line one\n"
$. #=> 1
f.lineno = 1000
f.lineno #=> 1000
$. # lineno of last read #=> 1
f.gets #=> "This is line two\n"
$. # lineno of last read #=> 1001
 
A

Ara.T.Howard

Hi

I've tried the following code on Win XP, Ruby 1.8rc1 and
Mac OS X 10.3, Ruby 1.6.8. From the online documentation I
pretty much copied and tried the following code:

f = File.new("testfile.txt")
puts f.gets >> This is line 1
puts f.gets >> This is line 2
f.lineno = 9 >>
puts f.lineno >> 9
puts f.gets >> This is line 3

Although f.lineno updates to 9, it never seems to affect
the line number next read.

if the file is small:


line = IO.readlines 'testfile.txt'

p line[9]
Thanks
John
______________________________________________________________
Herbalife Independent Distributor http://www.healthiest.co.za

--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| URL :: http://www.ngdc.noaa.gov/stp/
| TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done
===============================================================================
 

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
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top