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:
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]
路路路
il Sat, 24 Jul 2004 18:29:59 +0800, polarpolar <polarpolar@infor.org> ha scritto::
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:
rand(n) returns an integer in the range 0...n, which is good for an
array index.
Gavin
路路路
On Saturday, July 24, 2004, 8:41:58 PM, gabriele wrote:
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]