如何在Lua中检查两个表(对象)是否具有相同的值 [英] How to check if two tables(objects) have the same value in Lua

查看:697
本文介绍了如何在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆