This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Dejected's profile picture

Read A Specific Line?

Started by Dejected, 23 September 2013 - 12:46 PM
Dejected #1
Posted 23 September 2013 - 02:46 PM
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?
Lyqyd #2
Posted 23 September 2013 - 02: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
Dejected #3
Posted 23 September 2013 - 03:01 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
Thank you very much!
Wasn't really expecting anyone to answer so fast.
Dejected #4
Posted 25 September 2013 - 06:00 PM

  for line in handle:lines() do
Can you explain this part a little more?
I don't know what that does
Grim Reaper #5
Posted 25 September 2013 - 06:44 PM

if handle then
This 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.
Dejected #6
Posted 25 September 2013 - 06:53 PM

if handle then
This 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.
That you, you cleared it up for me
Dejected #7
Posted 25 September 2013 - 06:54 PM
Now I Have Another Question

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?
Lyqyd #8
Posted 25 September 2013 - 07: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.
Dejected #9
Posted 25 September 2013 - 08:06 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.
Thanks I can use that but I was looking for a way to save emails into a file and if you don't have an email you can create one. then it connects to the server and checks if that email already exists or not.