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

calling variables from string names

Started by M4sh3dP0t4t03, 09 June 2013 - 09:13 AM
M4sh3dP0t4t03 #1
Posted 09 June 2013 - 11:13 AM
How can I call a variable from a string name? I mean like if I enter the name of a variable it returns the value.
LBPHacker #2
Posted 09 June 2013 - 11:26 AM
Solution #1:
_G[name]()
Where _G is the table the variable is member of. If the variable is local, you can't do this, unless you put it into a local table. If it's an ordinary function like fs.open, it's part of _G, a global table provided by Lua. name is a name like "fs.open" without parentheses.

Solution #2:
I think you want your program to work like the interactive Lua prompt. In that case you should loadstring the input. So…
local func = loadstring(name)
if not func then func = loadstring("return " .. name) end
if func then func() end
name is a name like "fs.open()" with parentheses.
M4sh3dP0t4t03 #3
Posted 09 June 2013 - 11:32 AM
thanks! That's all I needed.