|
Post by nizamaeros on Jun 11, 2018 18:59:44 GMT
Hi. I've an array of data stored in a file (i.e 1.txt).
So, the file's content is something like this :
781938912 988109019 182391839
What I'm trying to do is I want to open & read the file, and type the data on app line by line.
------------------------------------------------------
for i=1,2,1 do
fileName = scriptPath() .. "1.txt" fh,err = io.open(fileName, "r") line = fh:read("*a")
type(line)
end
------------------------------------------------------
I'm getting errors when running it. Please help me. Thanks!
|
|
|
Post by AnkuLua on Jun 27, 2018 15:08:19 GMT
To read file line by line, try following
fileName = scriptPath() .. "1.txt" fh,err = io.open(fileName, "r") if (err) then scriptExit("file opening failed") end
for line in fh:lines() do print(line) type(line) end
|
|
|
Post by khrpnv on Jun 28, 2018 5:30:55 GMT
To read file line by line, try following fileName = scriptPath() .. "1.txt" fh,err = io.open(fileName, "r") if (err) then scriptExit("file opening failed") end
for line in fh:lines() do print(line) type(line) end
Hi, I have the same question, only I do not execute the function type () if I specify type (line) if the variable is together there is type ("text") then the text is entered. My code: fileName=scriptPath().."nomers.txt" nomers = io.open(fileName, "r") for line in nomers:lines() do toast(line) click(addmes) wait(1) click(searchcont) wait(2) type(line) end also type () does not work if in nomers.txt there is text and not numbers What could be the problem?
|
|
|
Post by khrpnv on Jun 28, 2018 8:11:10 GMT
In my opinion, the function only works so type("text"). It does not handle variables, only in this format "text". If the function is written type(var) then it does not work, so it happens with me
|
|
|
Post by AnkuLua on Jun 28, 2018 12:52:53 GMT
There is one extra control character read from fh:lines. Need to remove the control character by line = string.gsub(line, "%c", "")
Otherwise, only the last line is typed.
fileName = scriptPath() .. "1.txt" fh,err = io.open(fileName, "r") if (err) then scriptExit("file opening failed") end
for line in fh:lines() do line = string.gsub(line, "%c", "") print(line) type(line) end
|
|
|
Post by khrpnv on Jun 28, 2018 15:38:11 GMT
There is one extra control character read from fh:lines. Need to remove the control character by line = string.gsub(line, "%c", "") Otherwise, only the last line is typed. fileName = scriptPath() .. "1.txt" fh,err = io.open(fileName, "r") if (err) then scriptExit("file opening failed") end
for line in fh:lines() do line = string.gsub(line, "%c", "") print(line) type(line) end
Thank you very much for your answer! Numbers are entered, but before the numbers there was a question.
|
|
|
Post by AnkuLua on Jun 28, 2018 16:03:09 GMT
Suggest to edit the input file "1.txt" with Android app Or remove the "?" with following
fileName = scriptPath() .. "1.txt" fh,err = io.open(fileName, "r") if (err) then scriptExit("file opening failed") end
for line in fh:lines() do line = string.gsub(line, "%c", "") line = string.gsub(line, "?", "") print(line) type(line) end
|
|
|
Post by khrpnv on Jun 28, 2018 16:15:15 GMT
Suggest to edit the input file "1.txt" with Android app Or remove the "?" with following fileName = scriptPath() .. "1.txt" fh,err = io.open(fileName, "r") if (err) then scriptExit("file opening failed") end
for line in fh:lines() do line = string.gsub(line, "%c", "") line = string.gsub(line, "?", "") print(line) type(line) end
same result, the question "?" appears before the numbers:((
|
|
|
Post by khrpnv on Jun 28, 2018 21:52:13 GMT
I tried to print the line using the method line = string.sub (line, 2, 12) so that there was no question at the beginning. But this option does not give the desired result, "?" also appears at the beginning of the line. If to deduce "line" through a method print(line) that all ok. I await your comment on this issue.
|
|
|
Post by AnkuLua on Jun 28, 2018 23:03:00 GMT
|
|
|
Post by khrpnv on Jun 29, 2018 3:53:39 GMT
1.txt Once again, I note that through the methods print () and toast () numbers from the file 1.txt are printed correctly
|
|
|
Post by AnkuLua on Jun 29, 2018 11:29:00 GMT
|
|
|
Post by nizamaeros on Jul 12, 2018 12:10:25 GMT
Great thanks for all the replies!
But if I want to begin from line number 3, how can I do it?
|
|
|
Post by AnkuLua on Jul 12, 2018 14:45:17 GMT
fileName = scriptPath() .. "1.txt" fh,err = io.open(fileName, "r") if (err) then scriptExit("file opening failed") end
local lineNum = 1 for line in fh:lines() do if (lineNum >= 3) then line = string.gsub(line, "%c", "") line = string.gsub(line, "?", "") print(line) type(line) end lineNum = lineNum + 1 end
|
|
|
Post by dionvk on Jul 20, 2018 11:47:53 GMT
hello, base on this thread, how to random pull line? exp: from 1.txt : 781938912 988109019 182391839
in app randomize input "988109019" (second line)
|
|