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

Serialize/ Unserialize problem

Started by compaman, 21 April 2015 - 06:11 PM
compaman #1
Posted 21 April 2015 - 08:11 PM
Problem: I have a two dimensional table, that looks like this:

Spoiler
a = {
	 name = "stone",
	 pos1 = 1,
	 pos2 = 1,
	 [etc...]
	}
b = {
	 name = "dirt",
	 pos1 = 1,
	 pos2 = 2,
	 [etc...]
	}
c = ...
d = ...
etc...
table = {a, b, c, [etc...]}

now, I want to save this table in a file:

Spoiler
function save(table, filename)
	  hWrite = fs.open(filename, "w")
	  for i = 1, len(table), 1 do --len() is a function I created to determine the length of a table
		   hWrite.write(textutils.serialize(table[i]))
		   hWrite.write("\n")
	  end
	  hWrite.write("--End--")
	  hWrite.close()
end

So far so good, this works very well… So heres the problem:
my load function looks like this:

Spoiler
function load(filename)
	  hRead = fs.open(filename, "r")
	  local i = 1
	  local ret = {]
	  while true do
		   local x = hRead.readLine(i)
		   if x == "--End--" then break end
		   ret[i] = textutils.unserialize(x)
		   i = i+1
	  hRead.close()
	  return ret
end

Now what I realized:
The serialize thing writes the table in paragraphs, and the reaLine() only reads one line…
So instead of '{name = "stone", pos1 = 1, pos2 = 2, [etc…]}' it just reads '{'.
Any good approach for fixing this?
Edited on 21 April 2015 - 06:13 PM
Dragon53535 #2
Posted 21 April 2015 - 08:15 PM
textutils.unserialize(hRead.readAll())
Edited on 21 April 2015 - 06:15 PM
Creator #3
Posted 21 April 2015 - 08:15 PM
do this:


tableToSerialize = {
   a,
   b,
...
}
toWrite = textutils.serialize(tableToSerialize)
After writing the table to a file do this:
local file = fs.open(PATH,"r")
local data = file.readAll()
file.close()
myTable = textutils.unserialize(data)

I hope I helped you.

ninja'd
Edited on 21 April 2015 - 06:16 PM
Creator #4
Posted 21 April 2015 - 08:21 PM
You actually have to serialize all the subTables in one big table in order for it to work
compaman #5
Posted 21 April 2015 - 08:24 PM
Thank you both so much, this readAll() really is useful… :D/>
Edited on 21 April 2015 - 06:26 PM
Creator #6
Posted 21 April 2015 - 08:32 PM
You are welcome.

Remeber, never be affraid to ask. OR To ask, never be affraid to.
InDieTasten #7
Posted 21 April 2015 - 09:16 PM
By the way, since I saw it as argument name in your first snippet, NEVER call any of your data variables "table".
The name is globally allocated to contain the lua standard library functions of tables. Otherwise, (and you can trust me with this) some day you will find yourself getting Attempt to index nil exceptions^^
KingofGamesYami #8
Posted 22 April 2015 - 12:55 AM
You actually have to serialize all the subTables in one big table in order for it to work

What? texutils.serialize handles tables within tables just fine.
Dragon53535 #9
Posted 22 April 2015 - 02:15 AM
You actually have to serialize all the subTables in one big table in order for it to work

What? texutils.serialize handles tables within tables just fine.
That it does, but what OP is doing is separating the table and then serializing.


I'll just give the basics


local tbl = {a,b,c}
local file = fs.open("file","w")
file.write(textutils.serialize(tbl))
file.close()
local hRead = fs.open("file","r")
tbl = textutils.unserialize(hRead.readAll())
hRead.close()