Jump to content




Basic file IO - Learn how to save data!

help lua

  • You cannot reply to this topic
46 replies to this topic

#41 Brod8362

  • Members
  • 45 posts

Posted 20 July 2015 - 04:08 AM

Is it possible to read the data in individual Variables instead of in a table?

#42 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

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 Lemmmy

  • Members
  • 218 posts

Posted 23 July 2015 - 10:20 PM

View PostDragon53535, 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.


#44 biggest yikes

  • Members
  • 573 posts

Posted 26 July 2015 - 09:31 PM

View PostMtdj2, on 25 September 2012 - 02:26 PM, said:

You could instead of the loop use;
tFile = file.readAll()
and acsess the first line like:
print(tFile[1])
Hope I helped... Well, I think I did.
*reads post from 2 years ago that's incorrect*
Well, crap.

#45 pb2007

  • Members
  • 6 posts

Posted 13 February 2018 - 08:36 PM

Actually, it wouldn't be
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.


#46 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 13 February 2018 - 08:44 PM

View Postpb2007, on 13 February 2018 - 08:36 PM, said:

because computers count from 0, not 1
Allow me to introduce you to the wonderful world of Lua (and R, and maybe other languages).

#47 Saldor010

  • Members
  • 467 posts
  • LocationThe United States

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.

Edited by Saldor010, 13 February 2018 - 08:47 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users