【发布时间】:2014-06-17 11:44:54
【问题描述】:
首先,让我解释一下我的设置。我有 2 个 lua 文件:一个是要运行的主程序,另一个是配置文件。在主程序中,它会运行一次配置文件并将所有表加载到内存中。如果我想添加一个新配置,我不想重新编写主程序,我希望能够在配置文件中添加一个新表。
主程序:
-- Read config
dofile("config.lua")
-- Ask for ID
inputtedID = io.read()
-- Check if it exists
if string.format("%s[enabled]", inputtedID) then
-- If table exists and enabled key is set to true
print('"' .. inputtedID .. '" does exist!')
else
-- If the table doesn't exist or the key is false
print('"' .. inputtedID .. '" does not exist')
end
配置文件:
Public = {
enabled = true,
directory = "whatever"
}
Snapshot = {
enabled = true,
directory = "something"
}
-- So forth and so on
每个表都有相同的要读取的键,但我们需要关注的是enabled 键。这将我们带到了这个......
我希望它接受用户输入,检查是否存在与用户输入匹配的表,并检查该表中的enabled 键值是否为true。如果表不存在或值为 false,则只需运行一组不同的代码要求重试。
具体来说,我遇到困难的地方是,每当我在引用或使用表格时尝试使用变量时。不知道怎么做,也不知道应该怎么做。
【问题讨论】:
标签: function variables lua key-value lua-table