Lua Compare two tables to see if they are equal

Lua Compare two tables to see if they are equal

--Prerequisite: the key in both tables must be the same

function cmpTable(tab1, tab2)
	local eq_key = {}

	for k1, v1 in pairs(tab1) do
		local v2 = tab2[k1]
		if not v2 then
			return false
		end
		if v1["key1"] ~= v2["key1"] or v1["key2"] ~= v2["key2"] then
			return false
		end
		eq_key[k1] = k1
	end

	for k2, v2 in pairs(tab2) do
		if not eq_key[k2] then
			local v1 = tab1[k2]
			if not v1 then
				return false
			end
			if v1["key1"] ~= v2["key1"] or v1["key2"] ~= v2["key2"] then
				return false
			end
		end
	end

	return true
end

Comments

Popular posts from this blog

Python Receiving and parse JSON Data via UDP protocol

ubus lua client method and event registration code demo/example