49 posts
Posted 27 April 2016 - 01:36 AM
Hello everyone.
I trying to read lines from one file. This certain file have:
FIRST FILE
Hayden
Joao
Maria
Urso
And so on… because i can add more names if i want.
In my program, i need to read this lines to compare with the name in the file inside de Floppy disk:
Lets think in this example, this variable
ler = "Hayden"
Ok…
Now i want to compare with the
first file if the variable "ler" is inside in any of the lines of the "FIRST FILE".
hFile = fs.open("configs/gerentes", "r")
repeat
texto[i] = hFile.readLine()
i = i + 1
until texto[i] == "" or lines[i] == nil -- THIS LINE IS GIVING ERROR!
hFile.close()
for k, v in pairs(lines) do
if lines[v] == "--bler" then
lines[v] = ""
end
end
if ler == v then
-- continue etc...
I am getting error in this line:
texto[i] = hFile.readLine()
Edited on 27 April 2016 - 12:00 AM
756 posts
Posted 27 April 2016 - 01:48 AM
Well, you didn't give the exact error line, but the two only possible errors with this line are both "Attempt to index nil"
It's either because you haven't declared the texto table anywhere, add texto = {} somewhere at the top of your code,
or hFile is nil, meaning "configs/gerentes" can't be opened.
Edited on 26 April 2016 - 11:54 PM
49 posts
Posted 27 April 2016 - 02:00 AM
Well, you didn't give the exact error line, but the two only possible errors with this line are both "Attempt to index nil"
It's either because you haven't declared the texto table anywhere, add texto = {} somewhere at the top of your code,
or hFile is nil, meaning "configs/gerentes" can't be opened.
Look again. Error updated.
3057 posts
Location
United States of America
Posted 27 April 2016 - 02:20 AM
Anavrins correctly guessed your issue. Either texto or hFile is nil.
7083 posts
Location
Tasmania (AU)
Posted 27 April 2016 - 02:24 AM
You've now got two lines in your post saying different lines in the code are erroring. You're still failing to quote
the actual error.
I notice you're trying to treat "lines" as a table. That'll fail for the exact same reason treating "texto" as a table will fail - you haven't declared
either as such. It looks like you only want to use one of the two variable names.
Even if you fix that, your logic is still broken in that you're trying to compare against "v" outside of the loop "v" is localised to. You'd need to do it inside the "for" loop that iterates through lines/texto/whatever.
FWIW, I'd write things like this:
local ler, found = "Hayden", false
for line in io.lines("configs/gerentes") do
if line == ler then
found = true
break
end
end
if found then ...
49 posts
Posted 27 April 2016 - 03:43 AM
You've now got two lines in your post saying different lines in the code are erroring. You're still failing to quote
the actual error.
I notice you're trying to treat "lines" as a table. That'll fail for the exact same reason treating "texto" as a table will fail - you haven't declared
either as such. It looks like you only want to use one of the two variable names.
Even if you fix that, your logic is still broken in that you're trying to compare against "v" outside of the loop "v" is localised to. You'd need to do it inside the "for" loop that iterates through lines/texto/whatever.
FWIW, I'd write things like this:
local ler, found = "Hayden", false
for line in io.lines("configs/gerentes") do
if line == ler then
found = true
break
end
end
if found then ...
I've tryied this:
hFile = fs.open("configs/gerentes", "r")
for line in hFile.readLine() do -- ERROR IN THIS LINE
if line == ler then local passou = true end
hFile.close()
but the error is:
attempt to call string
7083 posts
Location
Tasmania (AU)
Posted 27 April 2016 - 04:08 AM
Sticking an iterator function into a "for" loop doesn't work like that. You have to specifically provide a function - io.lines() returns one, hFile.readLine() doesn't. Hence if you wanted to do things that way, you'd provide the function pointer itself without calling it yourself:
local ler, passou, hFile = "Hayden", false, fs.open("configs/gerentes", "r")
for line in hFile.readLine do --# Note lack of brackets; function is called by the loop itself.
if line == ler then
passou = true
break
end
end
hFile.close()
if passou then ...
49 posts
Posted 27 April 2016 - 06:03 PM
Hello people.
How can i read each line in a file and save as in variable, dont creating each variable to each line, because the lines can be multiple.
Example:
FILE 1:
Hayden Almeida
Tio cocota
Maria
and the program i am executing, i want to save each line separetly.
Atention: the "FILE 1" can contain more then these 3 names… so i need a loop to read until dont have more names in it.
Edited on 27 April 2016 - 06:18 PM
756 posts
Posted 27 April 2016 - 06:07 PM
Something like
local file = fs.open(path, "r")
local lines = {}
for line in file.readLine do
table.insert(lines, line)
end
Will result in the
lines table containing each lines of your file, lines[1] will contain "Hayden Almeida", lines[2] will have "Tio cocota", etc…
Edited on 27 April 2016 - 04:12 PM
49 posts
Posted 27 April 2016 - 06:10 PM
Something like
local file = fs.open(path, "r")
local lines = {}
for line in file.readLine do
table.insert(lines, line)
end
Will result in the table lines containing each line of your file, lines[1] will be the first line, lines[2] will be the second, etc…
And how can i compair with some name, like this??
if lines[1] == "Hayden Almeida" then
756 posts
Posted 27 April 2016 - 06:13 PM
And how can i compair with some name, like this??
if lines[1] == "Hayden Almeida" then
Exactly.
49 posts
Posted 27 April 2016 - 06:15 PM
And how can i compair with some name, like this??
if lines[1] == "Hayden Almeida" then
Exactly.
ERROR in this line:
for line in hFile.readLine() do
attempt to call string
MY CODE:
hFile = fs.open("configs/gerentes", "r")
local lines = {}
for line in hFile.readLine() do
table.insert(lines, line)
end
hFile.close()
Edited on 27 April 2016 - 04:17 PM
756 posts
Posted 27 April 2016 - 06:19 PM
You put brackets after readLine, they shouldn't be there…
3057 posts
Location
United States of America
Posted 27 April 2016 - 06:30 PM
More on the generic for loop
here.
749 posts
Location
BOO!!
Posted 27 April 2016 - 07:09 PM
Maybe gmatch('\n')
This will tell you to make a table containing each line of the file, but excludes \\n
Then you just type line = lines[80000000]
49 posts
Posted 27 April 2016 - 07:11 PM
You put brackets after readLine, they shouldn't be there…
for line in hFile.readLine() do
Where are brackets here?
2679 posts
Location
You will never find me, muhahahahahaha
Posted 27 April 2016 - 07:33 PM
Maybe gmatch('\n')
This will tell you to make a table containing each line of the file, but excludes \\n
Then you just type line = lines[80000000]
More like :
local lines = {}
local file = fs.open(path, "r")
local data = file.readAll()
file.close()
for token in gmatch("[^\n]+") do
lines[#lines+1] = token
end
749 posts
Location
BOO!!
Posted 27 April 2016 - 07:39 PM
More like :
lines[#lines+1] = token
Try table.insert
2679 posts
Location
You will never find me, muhahahahahaha
Posted 27 April 2016 - 07:56 PM
More like :
lines[#lines+1] = token
Try table.insert
More like you did not get the point of my correction.
By the way, table.insert is just a wrap around this:
lines[#lines+1] = token
756 posts
Posted 27 April 2016 - 08:14 PM
for line in hFile.readLine() do
Where are brackets here?
Just look at my code again, readLine shouldn't have () after it, that's why your code errors.
Edited on 27 April 2016 - 06:14 PM
49 posts
Posted 27 April 2016 - 08:17 PM
SOLVED!
This works:
local lines = {}
local hFile = fs.open("configs/gerentes", "r")
for lines in hFile.readLine do
if lines == ler then passou = true end
print(linha)
sleep(0.5)
end
hFile.close()
7083 posts
Location
Tasmania (AU)
Posted 28 April 2016 - 02:19 AM
Threads merged… You don't need two for the one question.