如何检查两个表(对象)在Lua中是否具有相同的值 [英] How to check if two tables(objects) have the same value in Lua
问题描述
我想在 Lua 中检查两个表是否具有相同的值,但没有找到方法.
I wanna check if two tables have the same value in Lua, but didn't find the way.
我使用运算符==
,它似乎只是检查相同的对象,而不是表中的元素.
I use the operator ==
, it seems just to check the same objects, but not the elements in the table.
如果我有两张桌子,
a={}
b={}
a==b
的值为 false
.
但是如果
a={}
b=a
a==b
的值为 true
.
我想知道是否有办法检查 Lua 中具有相同元素的两个表.是否有像 table.equals()
这样的内置函数来检查?
I wonder know if there a way to check two tables having the same elements in Lua. Is there a built-in function like table.equals()
to check?
推荐答案
没有用于按内容比较表格的内置函数.
There is no built-in function for comparing tables by contents.
你必须自己写.您需要决定是要浅层还是深入地按内容比较表格.请参阅 https://web.archive.org/web/20131225070434/http://snippets.luacode.org/snippets/Deep_Comparison_of_Two_Values_3 一些想法.
You'll have to write your own. You'll need to decide whether you want to compare tables by contents shallowly or deeply. See https://web.archive.org/web/20131225070434/http://snippets.luacode.org/snippets/Deep_Comparison_of_Two_Values_3 for some ideas.
这篇关于如何检查两个表(对象)在Lua中是否具有相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!