The number of lines of a file?

P

polarpolar

Hi, Ruby Fans :

I would like to write a simple program, which can read random line:
( file.txt has 7 lines )

1: srand 5269
2: f = File.new( "file.txt", "r" )
3: print f.readlines[ rand(6) ]

Thus I can get one line in file.txt by random. ( of course easily. )
Still, the "file.txt" will not only be a 7-lines file, right ? ;)
How can I get the number of lines of a file ?
So that I can get one line in file.txt by random
more flexibly just like:

1: srand 5269
2: f = File.new( "file.txt", "r" )
3: print f.readlines[ rand( lines_of_file-1 ) ]

However, I don't want to "loop" the loop :(
Is there any object or method I can use ?

Thanks,

polarpolar
(Is Ruby a singer?)
 
G

gabriele renzi

Hi, Ruby Fans :

I would like to write a simple program, which can read random line:
( file.txt has 7 lines )

1: srand 5269
2: f = File.new( "file.txt", "r" )
3: print f.readlines[ rand(6) ]

mh.. aFile.is_a? Enumerable, use Enumerable.to_a :)
Thus I can get one line in file.txt by random. ( of course easily. )
Still, the "file.txt" will not only be a 7-lines file, right ? ;)
How can I get the number of lines of a file ?

you have to read it. The basic concept of "line" is determined by how
you define it in your program.
So that I can get one line in file.txt by random
more flexibly just like:

1: srand 5269
2: f = File.new( "file.txt", "r" )
3: print f.readlines[ rand( lines_of_file-1 ) ]
However, I don't want to "loop" the loop :(
Is there any object or method I can use ?

dunno what you mean by "loop the loop", sorry.

Anyway this may work:
words=File.read('w.txt').split("\n")
word= words[rand(words.size)-1]
 
P

polarpolar

gabriele renzi æ到:
Anyway this may work:
words=File.read('w.txt').split("\n")
word= words[rand(words.size)-1]

Thank you.
I will learn more about the "line" :)

polarpolar
 
G

Gavin Sinclair

So that I can get one line in file.txt by random
more flexibly just like:

1: srand 5269
2: f = File.new( "file.txt", "r" )
3: print f.readlines[ rand( lines_of_file-1 ) ]
However, I don't want to "loop" the loop :(
Is there any object or method I can use ?
dunno what you mean by "loop the loop", sorry.
Anyway this may work:
words=File.read('w.txt').split("\n")
word= words[rand(words.size)-1]

I think you want

lines = File.readlines('w.txt')
random_line = lines[rand(lines.size)]

rand(n) returns an integer in the range 0...n, which is good for an
array index.

Gavin
 
M

Mark Sparshatt

polarpolar said:
Hi, Ruby Fans :

I would like to write a simple program, which can read random line:
( file.txt has 7 lines )

1: srand 5269
2: f = File.new( "file.txt", "r" )
3: print f.readlines[ rand(6) ]

Thus I can get one line in file.txt by random. ( of course easily. )
Still, the "file.txt" will not only be a 7-lines file, right ? ;)
How can I get the number of lines of a file ?
So that I can get one line in file.txt by random
more flexibly just like:

1: srand 5269
2: f = File.new( "file.txt", "r" )
3: print f.readlines[ rand( lines_of_file-1 ) ]
this way should work

srand 5269
lines = File.readlines("file.txt")
print lines[rand(lines.length)]

it reads the file into an array and then uses the length of the array

HTH
 
G

gabriele renzi

il Sat, 24 Jul 2004 20:07:31 +0900, Gavin Sinclair
I think you want

lines = File.readlines('w.txt')
random_line = lines[rand(lines.size)]

rand(n) returns an integer in the range 0...n, which is good for an
array index.

oops about rand(). OTOH I was pointing out the 'line is a user defined
concept' thingy by not using readlines.
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top