Lua中的表连接

56

原始帖子

鉴于Lua中没有内置函数,我正在寻找一种允许我将表格连接在一起的函数。我已经搜索了很多并尝试了我遇到的所有解决方案,但似乎没有一个能正常工作。

情景如下:我正在使用嵌入应用程序的Lua。 应用程序的内部命令以表格形式返回值列表。

我尝试的是在循环中递归调用该命令,并将返回的值(再次以表格形式)附加到上一次迭代的表格中。


编辑

对于将来查看此帖子的人,请注意@gimf发布的内容。由于Lua中的表格与数组非常相似(即使在列表上下文中也是如此),因此没有真正正确的方法将一个表格附加到另一个表格中。最接近的概念是合并表格。请参见帖子“Lua - merge tables?”以获得相关帮助。


可能是重复问题:https://dev59.com/A3M_5IYBdhLWcg3wp1Cl。 您提到“循环递归”。您是否在寻找深度复制+合并? - gimpf
以下是我找到的提供解决方案的链接:http://ardoris.wordpress.com/2008/08/10/lua-merge-two-tables-awesome3-rc2-config/http://www.idevgames.com/forum/archive/index.php/t-10223.html尽管我理解每个方法的思路,但似乎都不可行。您有可行的解决方案吗? - John Mark Mitchell
gimpf,也许我没有表述清楚。合并表格和连接表格虽然相似但是非常不同。我感兴趣的是将一个表格附加到另一个表格上,因此使用了连接这个词。 - John Mark Mitchell
请查看我的编辑;提供三个Lua表格的示例(2个输入,1个输出)将非常有帮助。 - gimpf
15个回答

1
这里的其他解决方案存在三个问题:
  1. 不能处理包含键/值的表格
  2. 在合并时无法保持表格中元素的顺序
  3. 不能处理同时包含数字索引和基于键的索引的表格
这个解决方案是对@kaptcha提出的原始解决方案的变体,解决了上述缺点。
--- Function to merge/join tables
--- @param ... table List of tables to be merged
--- @return table Merged table
function MergeTables(...)
    local result = {}

    for i, tbl in ipairs({...}) do
      for k, v in pairs(tbl) do
        if type(k) ~= "number" then
          result[k] = v
        else
          table.insert(result, v)
        end
      end
    end

    return result
end

使用方法:

local t1 = { a={1}, b={b=2} }
local t2 = { c={3}, d={d=4} }
local tMerged = MergeTables(t1, t2)

0

我喜欢@Weeve Ferrelaine的简洁回答,但突变可能会引起许多问题,一般来说是不可取的。

无突变版本。
---@param t1 {}
---@param t2 {}
function TableConcat(t1,t2)
    local tOut = {}

    for i = 1, #t1 do
        tOut[i] = t1[i]
    end

    for i = #t1, #t1 + #t2 do
        tOut[i] = t2[i]
    end

    return tOut
end
原始实现,这就是改变t1的。
function TableConcat(t1,t2)
    for i=1,#t2 do
        t1[#t1+1] = t2[i]
    end
    return t1
end

0
-- Lua 5.1+
function TableAppend(t1, t2)
    -- A numeric for loop is faster than pairs, but it only gets the sequential part of t2
    for i = 1, #t2 do
        t1[#t1 + 1] = t2[i] -- this is slightly faster than table.insert
    end

    -- This loop gets the non-sequential part (e.g. ['a'] = 1), if it exists
    local k, v = next(t2, #t2 ~= 0 and #t2 or nil)
    while k do
        t1[k] = v -- if index k already exists in t1 then it will be overwritten
        k, v = next(t2, k)
    end
end

你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community

0
使用table.insert()函数
table1 = {
  "param1=value1",
  "param2=value2",
  "param3=value3"
}
table2 = {
  "param21=value1",
  "param23=value2",
  "param23=value3"
}
table.insert(table1, table.concat(table2, ","))
print(table.unpack(table1));

-1
使用 table.concat:

http://lua-users.org/wiki/TableLibraryTutorial

> = table.concat({ 1, 2, "three", 4, "five" })
12three4five
> = table.concat({ 1, 2, "three", 4, "five" }, ", ")
1, 2, three, 4, five
> = table.concat({ 1, 2, "three", 4, "five" }, ", ", 2)
2, three, 4, five
> = table.concat({ 1, 2, "three", 4, "five" }, ", ", 2, 4)
2, three, 4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接