How to check if two tables(objects) have the same value in Lua

后端 未结 6 1574
粉色の甜心
粉色の甜心 2021-01-04 02:00

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 no

6条回答
  •  攒了一身酷
    2021-01-04 02:53

    if you just want to compare 2 small tables, you may (ab)use inspect.

    local ins = require 'inspect'
    local assert_equal = require 'luassert' .equal
    
    assert_equal(ins({ 1 , b = 30 }), ins({ b = 30, 1 }))
    

    This approach takes advantage of that inspect sorts table elements when serializing an object. while cjson doesn't, which makes it unusable in this case.

提交回复
热议问题