将字符串转换为变量调用

huangapple go评论46阅读模式
英文:

Turning a string into a variable call

问题

I want to set a variable from a string. For example, if string = 'variable' and later on I wanna set variable, the variable inside string, to a new value. How would I do that?

I've heard about something like _G before a string value in some way, and somehow that should convert that into a variable call. I'm probably saying a nonsense but don't get me wrong, I've been a lot of time trying to get this to work and writing a lot of tests in vain.

英文:

I want to set a variable from a string. For example, if string = 'variable' and later on I wanna set variable, the variable inside string, to a new value. How would I do that?

I've heard about something like _G before a string value in some way, and somehow that should convert that into a variable call. I'm probably saying a nonsense but don't get me wrong, I've been a lot of time trying to get this to work and writing a lot of tests in vain.

答案1

得分: 3

请注意,您提供的文本中包含一些HTML实体编码,我会将它们翻译成普通的字符。以下是翻译后的内容:

不要将 string 用作变量名; 这将覆盖全局字符串表(如果 string 是全局的)或遮蔽全局字符串表(如果 string 是局部的)。我将使用 varname

只需将 varname 用作索引 _G 的键;毕竟,_G 只是一个普通的表:

local varname = "variable"
print(_G[varname]) -- 打印名为 "variable" 的全局变量的值
_G[varname] = 42 -- 更改值

同样,要列出所有全局变量,只需使用 pairs 循环遍历 _G

for varname in pairs(_G) do print(varname) end

如果要调用值,请使用 _G[varname]()

function foo() print("foo!") end
function bar() print("bar!") end
local varname = "foo"
_G[varname]() -- foo!

根据您的具体情况,您可能希望在 _ENV / getfenv(1) 中查找变量(不一定是 _G),或使用 debug.getlocaldebug.getupvalue 分别搜索局部变量和上值。

不过,这似乎是一个X-Y问题:为什么您需要动态索引全局表?如果您不是使用设计不当的API的消费者,应该能够重新设计您的API,以使用自己的表而不是污染全局表。例如,我会将上述示例重写为:

local t = {}
function t.foo() print("foo!") end
function t.bar() print("bar!") end
local varname = "foo"
t[varname]() -- foo!
英文:

Don't use string as a variable name; you're overwriting (if string is global) or shadowing (if string is local) the global string table. I'll be using varname.

Simply use varname as a key to index _G; _G is just a regular table after all:

local varname = "variable"
print(_G[varname]) -- print the value of the global variable named "variable"
_G[varname] = 42 -- change the value

similarly, to list all global variables, you can just loop over _G using pairs:

for varname in pairs(_G) do print(varname) end

If you want to call the value, use _G[varname]():

function foo() print"foo!" end
function bar() print"bar!" end
local varname = "foo"
_G[varname]() -- foo!

Depending on your specifics, you might instead want to look up the variable in _ENV / getfenv(1) (which need not necessarily be _G) or search the local variables and upvalues using debug.getlocal and debug.getupvalue respectively.

This seems like an X-Y-Problem though: Why do you need to dynamically index the global table? If you're not the consumer of a poorly designed API, you should be able to re-engineer your API to use its own table rather than polluting the global table. For example, I'd rewrite the above example as:

local t = {}
function t.foo() print"foo!" end
function t.bar() print"bar!" end
local varname = "foo"
t[varname]() -- foo!

huangapple
  • 本文由 发表于 2023年5月15日 00:13:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248506.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定