lualua-table

How to check if two tables(objects) have the same value in 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.

If I have two tables,

a={}
b={}

the value of a==b is false.

but if

a={}
b=a

the value of a==b is true.

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?


Solution

  • There is no built-in function for comparing tables by contents.

    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.