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

Get variable from Table

Started by danielsv03, 18 April 2017 - 03:48 AM
danielsv03 #1
Posted 18 April 2017 - 05:48 AM
So i'm trying to get an variable from a table and this is how i'm trying to do it but when i call print it prints blank

Retrive and load table code (Not working)



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

tabl = load("root/system/configs/AppPos.conf")

print(tabl)
print(ypos)
print(xpos)

This is the table (Think its working)


{
  app = "Lua_Shell",
  xpos = "10",
  ypos = "5",
}
TheRockettek #2
Posted 18 April 2017 - 09:44 AM
load is already a function in lua.
Wilobate #3
Posted 18 April 2017 - 12:20 PM
print(tabl.xpos)
print(tabl.ypos)

I believe that should work
hbomb79 #4
Posted 19 April 2017 - 05:44 AM
The content of AppPos.conf would be helpful, perhaps the un-serialization is failing.

This clearly won't work as you are not indexing the table, instead you are printing 'xpos' and 'ypos' in the environment.

print( xpos )
print( ypos )

What happens if you change them to

print( tabl.xpos )
print( tabl.ypos )

If 'attempt to index boolean/nil/?' appears, this indicates that 'tabl' is not a table, so 'textutils.unserialise' is failing.

What does `print( type( tabl ) )` output? If it anything but 'table' then the AppPos.conf is likely invalid so textutils.unserialise cannot make a table out of it.
danielsv03 #5
Posted 19 April 2017 - 02:10 PM
The content of AppPos.conf would be helpful, perhaps the un-serialization is failing.

This clearly won't work as you are not indexing the table, instead you are printing 'xpos' and 'ypos' in the environment.

print( xpos )
print( ypos )

What happens if you change them to

print( tabl.xpos )
print( tabl.ypos )

If 'attempt to index boolean/nil/?' appears, this indicates that 'tabl' is not a table, so 'textutils.unserialise' is failing.

What does `print( type( tabl ) )` output? If it anything but 'table' then the AppPos.conf is likely invalid so textutils.unserialise cannot make a table out of it.

print(tabl.xpos)
print(tabl.ypos)

I believe that should work


Thanks you 2 it works now by doing table.xpos!

Thanks