表在Lua中的串联 [英] Concatenation of tables in Lua
问题描述
原始帖子
鉴于Lua中没有内置函数,我正在寻找一个允许我将表附加在一起的函数.我已经在Google上搜索了很多,并尝试了偶然发现的所有解决方案,但似乎都无法正常工作.
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.
推荐答案
答案过于复杂了吗?
这是我的实现方式
function TableConcat(t1,t2)
for i=1,#t2 do
t1[#t1+1] = t2[i]
end
return t1
end
这篇关于表在Lua中的串联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!