print("haicoder(www.haicoder.net)\n") t1 = {1, 3, 5, 7, 9} t2 = {[0]=1, 1, 3, 5, 7, 9} if #t1 ~= #t2 then print("not equal!") return end for i inpairs(t1) do if t1[i] ~= t2[i] then print("not equal!") return end end for i inpairs(t2) do if t1[i] ~= t2[i] then print("not equal!") return end end print("equal!")
table.equal=function(...) localfunctionequal2(t1,t2) if #t1~=#t2 returnfalseend for key,value inpairs(t1) iftype(value)=="table"andtype(t2[key])=="table" ifnot equal2(value,t2[key]) returnfalse end else if value~=t2[key] returnfalse end end end returntrue end for eachKey,eachTable inipairs{...} for eachKey2,eachTable2 inipairs{...} if eachKey~=eachKey2 andnot equal2(eachTable,eachTable2) returnfalse end end end returntrue end
table.equal=function(...) localfunctionequal2(t1,t2) if #t1~=#t2 returnfalseend for key,value inpairs(t1) iftype(value)=="table"andtype(t2[key])=="table" ifnot equal2(value,t2[key]) returnfalse end else if value~=t2[key] returnfalse end end end returntrue end
1 2 3 4 5 6 7
for _,eachTable inipairs{...} ifnot equal2({...}[1],eachTable) then returnfalse end end returntrue end
for key,value inpairs(t2) iftype(value)=="table"andtype(t1[key])=="table" ifnot equal2(value,t1[key]) returnfalse end else if value~=t1[key] returnfalse end end end
-- AndroLua 中的 table.size 在原生 Lua 中不可用,所以自己实现一个 -- 用 AndroLua 的可以去掉 table.size=function(t) iftype(table)~="table"thenerror("bad argument #1 to 'size' (table expected got "..type(table)..")") end local count=0 for _ inpairs(t) do count=count + 1end return count end
table.equal=function(...) local allTable={...} localfunctionequalTwo(t1,t2) if t1==t2 thenreturntrueend iftable.size(t1)~=table.size(t2) thenreturnfalseend for key,value inpairs(t1) do iftype(value)=="table"andtype(t2[key])=="table"then ifnot equalTwo(value,t2[key]) then returnfalse end else if value~=t2[key] then returnfalse end end end returntrue end for index=2,#allTable do ifnot equalTwo(allTable[1],allTable[index]) then returnfalse end end returntrue end
local t2={1,[3]=2,[4]={3},["5"]=4,["6"]={{{"5"}}}} local t2={1,[3]=2,[4]={3},["5"]=4,["6"]={{{"5"}}}} local t3={1,[3]=2,[4]={3},["5"]=4,["6"]={{{"5"}}}} print(table.equal(t1,t2,t3))
local t1={1,2,3,4,5} local t2={[0]=1,1,3,4,5} print(table.equal(t1,t2))