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

Insert variables into tables [SOLVED]

Started by danielsv03, 01 May 2017 - 03:54 PM
danielsv03 #1
Posted 01 May 2017 - 05:54 PM
Hello so i'm trying to insert like name = "name here" into this table




{
  "yeee",
  ypos = 2,
  xpos = 2,
}



soo far i been trying to do this



function load(name)
local file = fs.open(name,"r")
local data = file.readAll()
file.close()
return textutils.unserialize(data)
end

function save(table,name)
local file = fs.open(name,"w")
file.write(textutils.serialize(table))
file.close()
end


tabl = load("root/system/Apps/Lau_Shell/Config.conf")
--root/system/configs/AppPos.conf
print(tabl)
print(tabl.ypos)
print(tabl.xpos)
lua_icon = paintutils.loadImage("root/system/Apps/Lau_Shell/lua_shell.icon")
paintutils.drawImage(lua_icon, tabl.ypos, tabl.xpos)
a = {name = hello)
tab = {tabl}
table.insert(tabl, a)

save(tabl, "root/system/Apps/Lau_Shell/Config.conf")


I want it too look like this


{
  "yeee",
  ypos = 2,
  xpos = 2,
  name = "name_here",
}



btw the path is correct it leads to the table

Thx
Edited on 01 May 2017 - 05:11 PM
Goof #2
Posted 01 May 2017 - 07:06 PM
Instead of

a = {name = hello) --# the variable 'hello' is not defined. (Use as string form "hello")
tab = {tabl} --# why define this if you do not use it?
table.insert(tabl, a) --# inserts a (Which is just an empty table) to the tabl reference

You could do

tabl.name = "Hello" --# Directly changes the name index of the table to the desired value ("Hello")

That would directly change the tabl reference and add/change the name to the desired string/value.

However I am not entirely sure what your problem is.

I also found a syntax error, which may just be a typo, but who knows :)/>

a = {name = hello)
                 ^
Edited on 01 May 2017 - 05:06 PM
danielsv03 #3
Posted 01 May 2017 - 07:10 PM
Instead of

a = {name = hello) --# the variable 'hello' is not defined. (Use as string form "hello")
tab = {tabl} --# why define this if you do not use it?
table.insert(tabl, a) --# inserts a (Which is just an empty table) to the tabl reference

You could do

tabl.name = "Hello" --# Directly changes the name index of the table to the desired value ("Hello")

That would directly change the tabl reference and add/change the name to the desired string/value.

However I am not entirely sure what your problem is.

I also found a syntax error, which may just be a typo, but who knows :)/>

a = {name = hello)
				 ^



Thanks for the Support it now works :D/> I'm so glad you could spend your time on helping me :D/> Thx a lot!