Loading strings for variable naming
#1
Posted 14 December 2012 - 07:44 AM
#2
Posted 14 December 2012 - 07:55 AM
local var = loadstring("return "..varname)()
I would go for a more robust method and use the environment table. Like this:local var = getfenv()[varname]
Edit: I'm on the phone, so I can't test this.
#3
Posted 14 December 2012 - 08:39 AM
local loadstring(var) = "hello"?
#4
Posted 14 December 2012 - 09:30 AM
Doyle3694, on 14 December 2012 - 08:39 AM, said:
local loadstring(var) = "hello"?
local varname = "blah"
loadstring("return "..varname)() = "hello"
I'm not sure that you can assign return values though, but I think lua supports it.loadstring puts the text in the body of a new function and returns a reference to that function. So if you do this:
local func = loadstring("return os.version")
local ver = func() -- now ver holds a reference to the variable os.version
print(ver)
ver = 1 -- reassign a number to ver
It would be translated to:local func = function() return os.version end local ver = func() -- now ver holds a reference to the variable os.version print(ver) ver = 1 -- reassign a number to ver
So in short this could be written as:
loadstring("return os.version")() = "hello"
You could also use the global table technique:getfenv()[varname] = "hello"
#5
Posted 14 December 2012 - 09:45 AM
--table to hold variables with strings as names
local userVars={}
while true do
print("enter blank to exit")
write("variable name >")
local name=read()
if name=="" then
break
end
if userVars[name] then
print("current value of '"..name.."' is "..userVars[name])
end
write("New value:")
local val=read()
userVars[name]=val
print("value set.")
end
print("final values:")
for k,v in pairs(userVars) do
print(k.."=\""..v.."\"")
end
Again depends on what you're doing, but it can be safer than the global table approach which could end up clobbering things you might not want it to.
#6
Posted 14 December 2012 - 10:05 AM
To Orwell: Thanks sense
#7
Posted 14 December 2012 - 10:12 AM
function makeVar(name)
return name
end
makeVar("lawl") = "herpaderp"
#8
Posted 14 December 2012 - 10:54 AM
Doyle3694, on 14 December 2012 - 10:12 AM, said:
function makeVar(name)
return name
end
makeVar("lawl") = "herpaderp"
function makeVar(varname, var)
getfenv()['varname']=var
end
makeVar("lawl","herpaderp")
or:function makeVar(varname, var)
loadstring("return "..varname)() = var
end
makeVar("lawl","herpaderp")
#9
Posted 14 December 2012 - 11:11 AM
#10
Posted 14 December 2012 - 11:58 AM
Lyqyd, on 14 December 2012 - 11:11 AM, said:
loadstring("return "..varname)()
These function calls give a reference to the variable with the name 'varname'. So if you return this, you return a reference to the variable with that name. Like this:testVar = {"I am a table."}
local function getVar(varname)
return loadstring("return "..varname)()
end
alias = getVar("testVar")
alias == testVar -- True
Now the variable 'alias' is the exact same variable as 'testVar'. It's quite clear to me that Doyle's code would just make the variable 'alias' equal the string "testVar".testVar = {"I am a table."}
local function getVar(varname)
return varname
end
alias = getVar("testVar")
alias == "testVar" -- True
#11
Posted 21 December 2012 - 01:27 AM
function varCheck(var, stan)
if var == nil then
loadstring("return "..var)() = stan
end
end
#12
Posted 21 December 2012 - 01:46 AM
You're concatenating a string and nil, trying to use that as an lua chunk in loadstring, calling the returned function, and setting that function = stan.
If you're trying to produce an error, then I think you've succeeded. But if you had something else in mind then perhaps not.
#13
Posted 21 December 2012 - 01:59 AM
If someone with experience can lead me the way on this one, I would be ever so greatful
#14
Posted 21 December 2012 - 04:18 AM
#15
Posted 21 December 2012 - 04:50 AM
#16
Posted 21 December 2012 - 05:47 AM
Doyle3694, on 21 December 2012 - 04:50 AM, said:
Quote
Returns the current environment in use by the function. f can be a Lua function or a number that specifies the function at that stack level: Level 1 is the function calling getfenv. If the given function is not a Lua function, or if f is 0, getfenv returns the global environment. The default for f is 1.
#17
Posted 21 December 2012 - 05:51 AM
I'm not sure how much you know of this but the environment table is a table holding all global variables. The table key is the variable name, the value obviously is the variable value. (The main environment table is _G). So, let's take a look at this code:
local tEnv = getfenv() -- put the environment table for this function in tEnv
local printFunc = tEnv['print'] -- get the value for the key 'print' out of tEnv, it's a function pointer in this case
print( tostring( printFunc == print ) ) -- print the equality of the two function pointers
printFunc( tostring printFunc == print ) ) -- use the function pointer to call print
tEnv['print']("Test!") -- call the function pointer directly on the retreived env. table
getfenv()['print']("Test!") -- call the function pointer directly on the returned env. table from getfenv()
Hmmm, sort of ninja'd by PixelToast. But _G is only the global table and won't always work. (e.g. os.run() gives the loaded program an empty environment table)
#18
Posted 21 December 2012 - 05:57 AM
use
_G[string]=somethingor
pcall(setfenv(function() code end,setmetatable({[string]=something},getfenv()))
if you are worried about os.run
#19
Posted 21 December 2012 - 06:00 AM
PixelToast, on 21 December 2012 - 05:57 AM, said:
use
_G[string]=something
Try this as a program:
x = "test" print( _G["x"] )That won't work because x won't be put in the global environment table, only in the one that the function has. (an empty one in the case of os.run)
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











