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

Table serialisation problems

Started by Dave-ee Jones, 24 July 2017 - 12:12 AM
Dave-ee Jones #1
Posted 24 July 2017 - 02:16 AM
Hey,

So I am trying to do something like this to save a table in a file:

local t = {
["Test"] = { 1, 2, 3 },
["Test2"] = { 1, 2, 3 }
}
local tC = {
["button"] = { bg = colors.red, fg = colors.blue },
["test"] = { bg = colors.white, fg = colors.black }
}
local f = fs.open("/test","w")
f.write(textutils.serialise(t))
f.close()
local f2 = fs.open("/colours","w")
f.write(textutils.serialise(tC))
f.close()

Then I try and read a file, saving it to an existing table like so:

local t = {}
local f = fs.open("/test","r")
t = textutils.unserialise(f.readAll())
f.close()

But when I do so, the tables come up very weirdly..

-- This is the results of loading the tables (keep in mind this is also what it looks like in the file that the table was saved to)
local t = {
[ "Test" ] = { 1, 2, 3 },
Test2 = { 1, 2, 3 }
}

local tC = {
button = { bg = colors.red, fg = colors.blue },
test = { bg = colors.white, fg = colors.black }
}

Notice how it removed the '["' '"]' ONLY in the last one of the first table and the whole second table. Why is it doing this? It is soo weird, and I can't fix it :/

It's like it decided halfway through to ignore strings in tables, however I haven't told it to. Is it a problem with the serialisation?

I have a clue of what it might be..Is it the fact that there is no spaces in the strings so it converts it to a "variable" table title instead? That's very annoying…

I have a clue of what it might be. Is it the fact there is no spaces inbetween words in those strings, so it ignores them as strings and converts to a "variable"?
Edited on 24 July 2017 - 12:13 AM
KingofGamesYami #2
Posted 24 July 2017 - 02:42 AM
There is no functional difference so I kind of wonder why you care?
Dave-ee Jones #3
Posted 24 July 2017 - 02:47 AM
There is no functional difference so I kind of wonder why you care?

Actually there is.
E.g.

-- O is an object that has stuff like this:
local tC = {
["button"] = { bg = colors.white, fg = colors.black }, -- Will work
button = { bg = colors.black, fg = colors.white } -- Won't work
}
local tObject = {
text = "This is a BUTTON.",
type = "button" -- remember how I had the colour for the button?
}
local function drawObject(o)
term.setTextColor(tC[o.type].fg) -- Sets the colour, based on the type - referring to it as a string
print(o.text)
end
test(o) -- This will draw "This is a BUTTON." in black text.

Serialising it breaks it therefore it cannot refer to it as a string. This is only one quick example.


Never mind, it does work. It's weird - I didn't think it would as it's not showing as a string..Ah well, thanks for the help.
Edited on 24 July 2017 - 01:03 AM