Is it possible to read the data in individual Variables instead of in a table?
46 replies to this topic
#41
Posted 20 July 2015 - 04:08 AM
#42
Posted 21 July 2015 - 07:43 PM
Yeah. But to be honest, it would take a lot longer of code than reasonable. If you want to set a variable to a specific line, just read the data into the table, and save your variable as the line you want.
local file = fs.open('file','r') local tbl = {} local line = file.readLine() repeat tbl:insert(line) line = file.readLine() until not line local linethree = tbl[3]
Edited by Dragon53535, 21 July 2015 - 07:43 PM.
#43
Posted 23 July 2015 - 10:20 PM
Dragon53535, on 21 July 2015 - 07:43 PM, said:
Yeah. But to be honest, it would take a lot longer of code than reasonable. If you want to set a variable to a specific line, just read the data into the table, and save your variable as the line you want.
local file = fs.open('file','r') local tbl = {} local line = file.readLine() repeat tbl:insert(line) line = file.readLine() until not line local linethree = tbl[3]
Why not save the serialized table?
local data = {}
function read()
local file = fs.open("file", "r")
data = textutils.unserialize(file.readAll())
file.close() -- don''t forget to close your files
end
data.writingTest1 = "Hello"
data.writingTest2 = "World"
data.writingTest3 = 123
function write()
local file = fs.open("file", "w")
file.write(textutils.serialize(data))
file.close()
end
Note: untested code written on the fly
Edited by LemonLake, 23 July 2015 - 10:22 PM.
#45
Posted 13 February 2018 - 08:36 PM
Actually, it wouldn't be
Sorry, don't know how to use inline code lol
filedata[1], it would be
filedata[0]because computers count from 0, not 1. At least I think. ComputerCraft might be different,
Sorry, don't know how to use inline code lol
Edited by pb2007, 13 February 2018 - 08:37 PM.
#47
Posted 13 February 2018 - 08:46 PM
3 year necro.. And no, Lua starts at 1 for table indexes. It is generally a safe assumption that the index starts at 0 (for most languages), but for Lua, this isn't the case.
Edit: got ninja'd, darn you, Squid.
Edit: got ninja'd, darn you, Squid.
Edited by Saldor010, 13 February 2018 - 08:47 PM.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











