在Lua中比较表,其中键是表

4

我需要比较两个表格是否相等,即内容完全相同。这两个表格都有表格作为键。

例如:

t1 = {{1,1},{2,2}}
t2 = {{1,1},{2,2}}
t3 = {{1,1},{2,2},{3,3}}

t1和t2应该相等,但t1和t3不应该相等。


7
这些表格中的键不是表格。 - lhf
你在编程过程中有什么问题吗?展示一下你的方法。 - Tom Blodget
如果键和值可以是任何结构体,那么这将非常复杂。我想你可以根据自己的数据设计一个结构体。 - 上山老人
3个回答

0
你所需要的是一个表格比较。这不是语言的内置函数,因为它有许多不同的实现方式。
常见的一种是深度比较。 以下函数将深度比较表格,它有第三个参数来选择是否忽略元表。
function deepcompare(t1, t2, ignore_mt)
    local ty1 = type(t1)
    local ty2 = type(t2)
    if ty1 ~= ty2 then
        return false
    end
    -- non-table types can be directly compared
    if ty1 ~= "table" and ty2 ~= "table" then
        return t1 == t2
    end
    -- as well as tables which have the metamethod __eq
    local mt = getmetatable(t1)
    if not ignore_mt and mt and mt.__eq then
        return t1 == t2
    end
    for k1, v1 in pairs(t1) do
        local v2 = t2[k1]
        if v2 == nil or not deepcompare(v1, v2) then
            return false
        end
    end
    for k2, v2 in pairs(t2) do
        local v1 = t1[k2]
        if v1 == nil or not deepcompare(v1, v2) then
            return false
        end
    end
    return true
end

更多信息请参见:https://web.archive.org/web/20131225070434/http://snippets.luacode.org/snippets/Deep_Comparison_of_Two_Values_3


0
另一种方法是按照在Lua编程中所示的方式对两个表进行序列化。这将生成一组字符串的输出,当运行时,会重新创建表。将序列化器的输出存储在一个表中,而不是将它们输出以进行比较。
一旦两个表都被序列化为一组字符串,就可以简单地将序列化表A中的所有行与序列化表B中的所有行进行比较,并删除它们之间的任何重复项。如果在处理表A结束时,在表A或表B中仍有任何行,则它们不相等。
将代码序列化为字符串表(从PIL修改)并比较两个表a和b:
function basicSerialize (o)
    if type(o) == "number" then
      return tostring(o)
    else   -- assume it is a string
      return string.format("%q", o)
    end
end

function save (name, value, saved, output)
    saved = saved or {}       -- initial value
    output = output or {}     -- initial value
    if type(value) == "number" or type(value) == "string" then
        table.insert (output, name .. " = " .. basicSerialize(value))
    elseif type(value) == "table" then
        if saved[value] then    -- value already saved?
            table.insert (output, name .. " = " .. saved[value])  -- use its previous name
        else
            saved [value] = name   -- save name for next time
            table.insert (output, name .. " = {}")     -- create a new table
            for k,v in pairs(value) do      -- save its fields
                local fieldname = string.format("%s[%s]", name, basicSerialize(k))
                save (fieldname, v, saved, output)
            end
        end
    else
        error("cannot save a " .. type(value))
    end
    return output
end

function compareSerializedTable (t1, t2)
    if (#t1 ~= #t2) then
        return false
    end

    for i = #t1, 1, -1 do
        local line = t1 [i]
        for k, comp in ipairs (t2) do
            if (line == comp) then
                table.remove (t1, i)
                table.remove (t2, k)
                break
            end
        end
    end

    return (#t1 == 0 and #t2 == 0)
end

t1 = {{1,1},{2,2}}
t2 = {{1,1},{2,2}}
t3 = {{1,1},{2,2},{3,3}}

o1 = save ('t', t1)
o2 = save ('t', t2)
o3 = save ('t', t3)

print (compareSerializedTable (o1, o2)) --true
print (compareSerializedTable (o1, o3)) --false

0

我的解决方案不是绝对的(不喜欢键),但应该可以处理您提出的嵌套表格问题。我的概念是递归和简单的:

从每个输入中取一个条目,确保它们:类型匹配,都是表格,并且两个表格具有相同的长度。如果这三个条件成立,则可以递归地进行1:1比较两个表格。如果类型不匹配或表格长度不同,则自动失败。

function compare (one, two)

    if type(one) == type(two) then
        if type(one) == "table" then
            if #one == #two then

                -- If both types are the same, both are tables and 
                -- the tables are the same size, recurse through each
                -- table entry.
                for loop=1, #one do
                    if compare (one[loop], two[loop]) == false then
                        return false
                    end
                end 

                -- All table contents match
                return true
            end
        else
            -- Values are not tables but matching types. Compare
            -- them and return if they match
            return one == two
        end
    end
    return false
end

do
    t1 = {{1,1},{2,2}}
    t2 = {{1,1},{2,2}}
    t3 = {{1,1},{2,2},{3,3}}

    print (string.format(
        "t1 == t2 : %s", 
        tostring(compare (t1,t2))))

    print (string.format(
        "t1 == t3 : %s", 
        tostring(compare (t1,t3))))
end

输出结果为:

t1 == t2 : true
t1 == t3 : false

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接