Lua中表格的串联 [英] Concatenation of tables in Lua
问题描述
原帖
鉴于 Lua 中没有内置函数,我正在寻找一个允许我将表附加在一起的函数.我在谷歌上搜索了很多,并尝试了我偶然发现的所有解决方案,但似乎没有一个能正常工作.
Given that there is no built in function in Lua, I am in search of a function that allows me to append tables together. I have googled quite a bit and have tried every solutions I stumbled across but none seem to work properly.
场景是这样的:我正在使用嵌入在应用程序中的 Lua.应用程序的内部命令以表格的形式返回值列表.
The scenario goes like this: I am using Lua embeded in an application. An internal command of the application returns a list of values in the form of a table.
我想要做的是在循环中递归调用该命令,并将返回的值再次以表格的形式附加到先前迭代的表格中.
What I am trying to do is call that command recursively in a loop and append the returned values, again in the form of a table, to the table from previous iterations.
编辑
对于以后遇到此帖子的人,请注意@gimf 发布的内容.由于 Lua 中的表比其他任何东西都更像数组(即使在列表上下文中),因此没有真正正确的方法将一个表附加到另一个表.最接近的概念是合并表.请参阅帖子Lua - 合并表?"以获得这方面的帮助.
For those who come across this post in the future, please note what @gimf posted. Since Tables in Lua are as much like arrays than anything else (even in a list context), there is no real correct way to append one table to another. The closest concept is merging of tables. Please see the post, "Lua - merge tables?" for help in that regard.
推荐答案
过于复杂的答案很多吗?
Overcomplicated answers much?
这是我的实现:
function TableConcat(t1,t2)
for i=1,#t2 do
t1[#t1+1] = t2[i]
end
return t1
end
这篇关于Lua中表格的串联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!