我的问题是如何(或是否)可以在lua表中插入两个值.
My question is how (or if) you can insert two values into a lua table.
我有个返回(可变数量的值)的函数
I got a function that returns (a variable number of values)
function a(x, y) return x, y end和另一个将点插入表中的函数
and another function that inserts that points into a table,
function b(x, y) table.insert(myTable, x, y) end那么我该如何使用可变数量的参数调用函数b并将它们全部插入到我的表中呢?
So how can i make, that i can call function b with a variable number of arguments and insert them all into my table?
推荐答案如果函数的最后一个参数是...(称为vararg函数),则Lua解释器会将所有多余的参数放入....您可以使用{...}将其转换为表,然后将键/值复制到名为myTable的全局表中.这是您的函数外观:
If the last parameter for your function is ... (called a vararg function), the Lua interpreter will place any extra arguments into .... You can convert it to a table using {...} and copy the keys/values into your global table named myTable. Here's what your function would look like:
function b(...) for k, v in pairs({...}) do myTable[k] = v end end b(1, 2) -- {[1] = 1, [2] = 2} is added to myTable您应该根据要替换,合并还是将元素添加到myTable来调整功能.
You should tweak the function depending on whether you want to replace, merge or append elements into myTable.
更多推荐
Lua将多个变量插入表中
发布评论