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

Table Handling

Started by metron80, 03 November 2014 - 12:46 AM
metron80 #1
Posted 03 November 2014 - 01:46 AM
Okay, so I have been trying to save items to a table. Currently, I am trying to save a serialized table to a file. Code:

local tabl = {"i1" = {iType = "Bread", amount = "5", price = 1, sId = 84},"i2" = {iType = "Wheat", amount = "64", price = 2, sId = 0}, "i3" = {iType = "Seeds", amount = "4 1/2 Stks", price = 5, sId = 0}}
file = fs.open("shop", "w")
file.write(textutils.serialize(tabl))
file.close()
print("h")
error()
After that, I get the error "bios:339: [string "startup"]:1: "}" expected." The table is fine, and I see no error. Any help would be appreciated, thanks.
Bomb Bloke #2
Posted 03 November 2014 - 02:17 AM
These are valid ways to declare keys in new tables:

local tabl = {i1 = {}}
local tabl = {["i1"] = {}}

… as opposed to:

local tabl = {"i1" = {}}
valithor #3
Posted 03 November 2014 - 02:20 AM
Okay, so I have been trying to save items to a table. Currently, I am trying to save a serialized table to a file. Code:

local tabl = {"i1" = {iType = "Bread", amount = "5", price = 1, sId = 84},"i2" = {iType = "Wheat", amount = "64", price = 2, sId = 0}, "i3" = {iType = "Seeds", amount = "4 1/2 Stks", price = 5, sId = 0}}
file = fs.open("shop", "w")
file.write(textutils.serialize(tabl))
file.close()
print("h")
error()
After that, I get the error "bios:339: [string "startup"]:1: "}" expected." The table is fine, and I see no error. Any help would be appreciated, thanks.

You are essentially trying to save a table to a string. The problem here is the "il" = when you define the table.

edit: ninjaed
Edited on 03 November 2014 - 01:20 AM
metron80 #4
Posted 03 November 2014 - 11:13 PM
Ok, so it would be this:

local tabl = {i1 = {iType = "Bread", amount = "5", price = 1, sId = 84}, i2 = {iType = "Wheat", amount = "64", price = 2, sId = 0}, i3 = {iType = "Seeds", amount = "4 1/2 Stks", price = 5, sId = 0}}
Not what was in the first post.
Bomb Bloke #5
Posted 04 November 2014 - 12:12 AM
Yep, that'll work just fine.
metron80 #6
Posted 04 November 2014 - 01:06 AM
Okay thanks! Appreciated! :)/>