如何将字符串转换为表格
例如,我加载了一个模块,该模块中有一个名为“Table1”的表。在主文件中,我有一个表,我希望它与“Table1”完全相同。 那么,如果我只有该表的名称,我该怎么做呢? 当我尝试这样做时,
str = "Table1"
t = str
我显然得到一个字符串而不是表格,那么如何获取该表格内容的表格内容呢?我想要的是能够以某种方式使这一行代码
t = 'Table1'
等同于这一行
t = Table1
For example I loaded a module, and there is a table in this module with name "Table1". In the main file I have a table which I want to be the exact same copy of "Table1".
So how can I do it, if I have only a name of that table.
When I am trying to do it like this
str = "Table1"
t = str
I obviously get a string instead of table, so how can I get a table content that table content? What I want is to able somehow make this line of code
t = 'Table1'
be equvalent to this one
t = Table1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
str
是全局变量的名称,请使用_G[str]
获取其值。If
str
is the name of a global variable, use_G[str]
to get its value.Lua 中的表是一种非常灵活且重要的数据类型。如此之多,甚至模块也是表。如果您知道模块中有一个给定名称的表,并且您在变量中拥有它的名称,则只需使用
[]
运算符即可获取该表:但是,这不是一个非常好的方法,因为它假设您“知道”该模块包含给定名称的表。您始终可以设计通过给定标准名称(不会更改)导出表的模块:
Tables in Lua are a very flexible and important datatype. So much, that even modules are tables. If you know, that there is a table by a given name in the module, and you have it's name in a variable, just use the
[]
operator to get the table:However, this is not a very good approach, because it assumes that you "know" that the module contains a table by the given name. You can always design modules that will export the table by a given standard name (which does not change):