I know about the fs.readLine when a file is open and it reads the next line, but is there a way to read a specific line from the file.
Like if i wanted to make a config for something and wanted to read from a certain line of that config how would I do that without having to use something like a filler variable?
Read A Specific Line?
Started by Dejected, Sep 23 2013 01:46 PM
8 replies to this topic
#1
Posted 23 September 2013 - 01:46 PM
#2
Posted 23 September 2013 - 01:50 PM
Read all of the lines into a table and then index the specific entry you're interested in:
local linesTable = {}
local handle = io.open("config", "r")
if handle then
for line in handle:lines() do
table.insert(linesTable, line)
end
handle:close()
end
local myLine = ""
if #linesTable > 0 then --# make sure the file was read
myLine = linesTable[3] --# get the third line
end
#3
Posted 23 September 2013 - 02:01 PM
Lyqyd, on 23 September 2013 - 01:50 PM, said:
Read all of the lines into a table and then index the specific entry you're interested in:
local linesTable = {}
local handle = io.open("config", "r")
if handle then
for line in handle:lines() do
table.insert(linesTable, line)
end
handle:close()
end
local myLine = ""
if #linesTable > 0 then --# make sure the file was read
myLine = linesTable[3] --# get the third line
end
Wasn't really expecting anyone to answer so fast.
#5
Posted 25 September 2013 - 05:44 PM
if handle thenThis is used to make sure that the handle isn't a nil reference, meaning that io.open returned nil. This stops us from doing things with a nil variable and causing and error.
for line in handle:lines() do table.insert (linesTable, line) end handle:close()'lines' is a function which is part of the table returned by io.open. It allows us to iterate over every line in the file. Each line is stored in the variable line and is then stored in the lines table at the next numerical index in 'linesTable'.
After the loop ends (all lines are iterated over), the handle is closed.
#6
Posted 25 September 2013 - 05:53 PM
Grim Reaper, on 25 September 2013 - 05:44 PM, said:
if handle thenThis is used to make sure that the handle isn't a nil reference, meaning that io.open returned nil. This stops us from doing things with a nil variable and causing and error.
for line in handle:lines() do table.insert (linesTable, line) end handle:close()'lines' is a function which is part of the table returned by io.open. It allows us to iterate over every line in the file. Each line is stored in the variable line and is then stored in the lines table at the next numerical index in 'linesTable'.
After the loop ends (all lines are iterated over), the handle is closed.
#7
Posted 25 September 2013 - 05:54 PM
If I want to store a new value in a config and keep the one there how would I do that?
Nevermind this is how I did it tell me if there is a better way
local options = {}
local x = io.open("Config","r")
for line in x:lines() do
table.insert(options,line)
end
x:close()
local x = fs.open("Config","w"(
for i = 1,#options do
x.writeLine(options[i])
end
ans = read()
x.writeLine(ans)
x.close()
That was only as a test but that works right?
#8
Posted 25 September 2013 - 06:54 PM
If you want to add a new value that you know doesn't exist in the file yet, you could open the file in append mode, write just the new value, and close it.
#9
Posted 25 September 2013 - 07:06 PM
Lyqyd, on 25 September 2013 - 06:54 PM, said:
If you want to add a new value that you know doesn't exist in the file yet, you could open the file in append mode, write just the new value, and close it.
3 user(s) are reading this topic
0 members, 3 guests, 0 anonymous users











