【问题标题】:Lua: Can I select a table in script via argument in command line?Lua:我可以通过命令行参数在脚本中选择一个表吗?
【发布时间】:2019-10-09 15:38:27
【问题描述】:

我有一个包含许多表格的 lua 脚本

local a = {1,2,3}
local b = {1,2,3}
local c = {1,2,3}

等等。和一个类似的功能

test = {}
function test.set(args)
    for x, y in pairs(args) do
    ....
    end
end

test[arg[1]](arg[2])

现在我想通过命令行选择其中一个表在函数中使用它。我试过了

lua MyScript.lua set a

我得到了错误

lua: MyScript.lua:1249: bad argument #1 to 'pairs' (table expected, got string)
stack traceback:
    [C]: in function 'pairs'
    MyScript.lua:1249: in field '?'
    MyScript.lua:1266: in main chunk
    [C]: in ?

这有点道理,因为我传递了一个字符串。但我不知道如何在函数中选择我要使用的表。有人可以帮帮我吗?

【问题讨论】:

    标签: function cmd lua


    【解决方案1】:
    local all_your_tables = {a = {1,2,3}, b = {1,2,3}, c = {1,2,3}}
    ...
    test[arg[1]](all_your_tables[arg[2]])
    

    【讨论】:

      猜你喜欢
      • 2013-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      • 2014-04-03
      • 1970-01-01
      相关资源
      最近更新 更多