Lua 中的表串联
原始帖子
鉴于Lua中没有内置函数,我正在寻找一个允许我将表附加在一起的函数。我在谷歌上搜索了很多,并尝试了我偶然发现的所有解决方案,但似乎没有一个能正常工作。
场景是这样的:我正在使用嵌入在应用程序中的 Lua。应用程序的内部命令以表格的形式返回值列表。
我想做的是在循环中递归地调用该命令,并将返回的值再次以表的形式附加到先前迭代的表中。
编辑
对于那些将来看到这篇文章的人,请注意@gimf 发布的内容。由于 Lua 中的表比其他任何东西都更类似于数组(即使在列表上下文中),因此没有真正正确的方法将一个表附加到另一个表。最接近的概念是表的合并。请参阅帖子“Lua - 合并表?”以获取这方面的帮助。
ORIGINAL POST
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.
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.
EDIT
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
答案过于复杂了很多吗?
这是我的实现:
Overcomplicated answers much?
Here is my implementation:
如果您想将现有表连接到新表,这是最简洁的方法:
尽管我不确定这在性能方面有多好。
If you want to concatenate an existing table to a new one, this is the most concise way to do it:
Although I'm not sure how good this is performance-wise.
还有一种方式:
在我看来,这是最具可读性的一种方式 - 它迭代第二个表并将其值附加到第一个表,即故事的结尾。好奇它的速度与上面的显式索引 [] 相比如何
And one more way:
It seems to me the most readable one - it iterates over the 2nd table and appends its values to the 1st one, end of story. Curious how it fares in speed to the explicit indexing [] above
一个简单的方法来做你想做的事:
A simple way to do what you want:
要将两个表添加在一起,请
使用第一个表作为要添加的变量,因为代码会将第二个表按顺序添加到第一个表的末尾。
i
是表或列表的起始编号。#secondtable+#firsttable
是结束的地方。它从要添加到的第一个表的末尾开始,到
for
循环中的第二个表的末尾结束,因此它适用于任何大小的表或列表。To add two tables together do this
use the first table as the variable you wanted to add as code adds the second one on to the end of the first table in order.
i
is the start number of the table or list.#secondtable+#firsttable
is what to end at.It starts at the end of the first table you want to add to, and ends at the end of the second table in a
for
loop so it works with any size table or list.一般来说,连接任意表的概念在 Lua 中没有意义,因为单个键只能有一个值。
在某些特殊情况下,串联确实有意义。其中之一是包含简单数组的表,这可能是旨在返回结果列表的函数的自然结果。
在这种情况下,您可以这样写:
这是一个浅拷贝,并且不会尝试找出
userdata
或函数值是否是可能需要不同处理的某种容器或对象。另一种实现可能会修改第一个参数而不是创建新表。这将节省复制成本,并使
array_concat
与字符串上的..
运算符不同。编辑:正如 Joseph Kingry 的评论中所观察到的,我未能正确提取
...
中每个参数的实际值。我也根本无法从函数返回合并表。这就是我在答案框中编码而不测试代码所得到的结果。In general the notion of concatenating arbitrary tables does not make sense in Lua because a single key can only have one value.
There are special cases in which concatenation does make sense. One such is for tables containing simple arrays, which might be the natural result of a function intended to return a list of results.
In that case, you can write:
This is a shallow copy, and makes no attempt to find out if a
userdata
or function value is a container or object of some kind that might need different treatment.An alternative implementation might modify the first argument rather than creating a new table. This would save the cost of copying, and make
array_concat
different from the..
operator on strings.Edit: As observed in a comment by Joseph Kingry, I failed to properly extract the actual value of each argument from
...
. I also failed to return the merged table from the function at all. That's what I get for coding in the answer box and not testing the code at all.如果您想合并两个表,但需要结果表的深层副本,无论出于何种原因,请使用 中的合并另一个关于合并表的问题以及来自lua-users的一些深度复制代码。
(编辑
好吧,也许您可以编辑您的问题以提供一个最小的示例...如果您的意思是
将一个表与另一个表连接起来
应该
,那么您就不走运了。 键是唯一的。
您似乎想要一个对的列表,例如
{ { a, 1 }, { b, 2 }, { a, 5 }, { b, 10 } }
。您还可以使用最终结构,例如{ a = { 1, 5 }, b = { 2, 10 } }
,具体取决于您的应用程序。但是“连接”表的简单概念对于 Lua 表来说没有意义。
)
If you want to merge two tables, but need a deep copy of the result table, for whatever reason, use the merge from another SO question on merging tables plus some deep copy code from lua-users.
(edit
Well, maybe you can edit your question to provide a minimal example... If you mean that a table
concatenated with another table
should result in
then you're out of luck. Keys are unique.
It seems you want to have a list of pairs, like
{ { a, 1 }, { b, 2 }, { a, 5 }, { b, 10 } }
. You could also use a final structure like{ a = { 1, 5 }, b = { 2, 10 } }
, depending on your application.But the simple of notion of "concatenating" tables does not make sense with Lua tables.
)
这是我完成的与上面的 RBerteig 类似的实现,但使用隐藏参数 arg,该参数在函数接收可变数量的参数时可用。就我个人而言,我认为这比 select 语法更具可读性。
Here is an implementation I've done similar to RBerteig's above, but using the hidden parameter arg which is available when a function receives a variable number of arguments. Personally, I think this is more readable vs the select syntax.
这是我连接一组纯整数索引表的实现,仅供参考。
concat_2tables
另一个递归函数
concatenateTables
:通过unpack
拆分表列表,并调用concat_2tables
连接table1
和restTableList
<前><代码>t1 = {1, 2, 3}
t2 = {4, 5}
t3 = {6}
concat_2tables = 函数(表1, 表2)
len = 表.getn(表1)
对于 key,val 成对(table2)do
表1[键+长度] = val
结尾
返回表1
结尾
concatenateTables = 函数( 表列表 )
如果 tableList==nil 那么
返回零
elseif table.getn(tableList) == 1 那么
返回表列表[1]
别的
表1 = 表列表[1]
RestTableList = {解压(tableList, 2)}
返回 concat_2tables(table1, concatenateTables(restTableList))
结尾
结尾
tt = {t1, t2, t3}
t = 连接表(tt)
Here is my implementation to concatenate a set of pure-integer-indexing tables, FYI.
concat_2tables
another recursive function
concatenateTables
: split the table list byunpack
, and callconcat_2tables
to concatenatetable1
andrestTableList
编辑
这是一个更好的解决方案,另一个倾向于覆盖数字键,用法仍然相同:
ORIGINAL
游戏有点晚了,但这似乎对我有用:
它可能有点过于复杂,但它需要无限量的参数,并且适用于键值对和常规“数组”(数字作为键)。这是一个示例
EDIT
Here's a better solution, the other one tended to overwrite numeric keys, the usage is still the same:
ORIGINAL
A wee bit late to the game, but this seems to work for me:
It might be a bit overcomplicated, but it takes an infinite amount of arguments, and works for both key-value pairs and regular "arrays" (numbers as keys). Here's an example
这里的其他解决方案遇到了 3 个问题:
该解决方案是@kaptcha 提出的原始解决方案的变体,它解决了上述缺点:
用法:
The other solutions here suffered from 3 issues:
This solution is a variant of the original solution proposed by @kaptcha which addresses the shortcomings noted above:
Usage:
我喜欢 @Weeve Ferrelaine 答案的简单性,但突变可能会导致许多问题总的来说都是不可取的。
没有突变的版本。
原始实现,即改变 t1。
I like the simplicity in @Weeve Ferrelaine answer, but mutations may cause many issues and in general, are not desirable.
Version with NO MUTATION.
Original implementation, that's mutating t1.
使用 table.insert() 函数
use table.insert() function
我连接表的方法
您可以传递表和非表对象,它会很乐意将它们合并到一个表中。
例子:
My approach on concatenating tables
You can pass tables and non-table objects and it will happily merge them into one table.
Example: