PtokaX forum

Archive => Archived 4.0 boards => Help with Lua 4 scripts => Topic started by: suyang99 on 07 January, 2004, 10:21:03

Title: help with handle the file
Post by: suyang99 on 07 January, 2004, 10:21:03
i want to read random line from the multiline file, because i want to search some data in line
how can i do for that ?(  
Title:
Post by: c h i l l a on 07 January, 2004, 11:32:33
--openfile in read mode
local handle = openfile(file,"r")

--read one line

local line = read(handle)

--see if line is there

while line do

--your stuff to do with line

--read next line

line = read(handle)

end

--closefilehandle

closefile(handle)
Title:
Post by: suyang99 on 07 January, 2004, 13:06:03
thx a lot
i know this method

i mean like this
i have 10 lines
i think only of to read the 6th

can u hlp me
Title:
Post by: c h i l l a on 07 January, 2004, 13:13:10
as always there are many ways...

but I think the simplest way would be you create a

variable

at the first line = read(handle)

set the varaible to zero

(ex:  var = 0)

and then always count one up each while

var = var + 1

then check if var is your search line

if var == 6 then
 --do what you want with line 6
  break
end

I would call that break cause you don't wanna read any futher.

other soltuion is bit more advanced but faster.
you need to seek all line beginnings once, and insert those seeks in a table, then you have your line positions and call them by seek again.
That what I use in my still now small trivia bot... but that way I have fast access to many lines, and don't have load all questions in a table :). Might post a preview later.
Title:
Post by: suyang99 on 07 January, 2004, 13:26:01
thx a lot
u have resolved my big problem

i try to seek all lines in a table but always
  Syntax Error and tell me attempt to concat a nil value
so i don't konw and should how do