LUA按时间戳比较两个表

时间:2014-04-17 08:40:32

标签: lua lua-table

我试图比较我创建的两张牌桌。 第一个表总是新的,其中有两个或三个项目。

表格如下:

existingMTable = {}
local mTable = {}
local user = "me"

local function setupTable()
    local e = {}

    for i = 0, #totalusers do 
        e.secSinceMyEvent = GetTimeSinceEvent(i)
        e.timestamp = GetTimeStamp(i)
        e.seller = GetUserNames(i)

        of e.seller = "me" then
        table.insert(mTable, e)
        valueExists(mTable, e.timestamp)
    end
end

local function valueExists(tbl, value)

    for k,v in pairs(tbl) do
        if value ~= v then
            table.insert(existingMTable.History, value)

           print("value" .. value)
           print("v" .. v)
           print(existingMTable.History.timestamp)
        end
    end

    return false
end

我过滤掉了很多人,我只想要1个用户的项目。 一旦我有几件商品,我想将其添加到他的销售历史中。

我需要将3个以上的新项目与他当前的表格进行比较吗?我相信时间戳。

1 个答案:

答案 0 :(得分:1)

你正在做的事情没有意义,因为valueExists的第二个参数是timestamp,它变为"值"在valueExists中,但在此功能中将是" e"类型项目。如果我按照你调用它的方式重命名并更改一些名称以使其保持一致,那么你会有类似的东西:

local function valueExists(tbl, timestamp)

    for k,e in ipairs(tbl) do
        if timestamp ~= e.timestamp then
            table.insert(existingMTable.History, e)

            print("value" .. timestamp)
            print("e.timestamp" .. e.timestamp)
            #print(existingMTable.History.timestamp) -- WRONG because you inserted an e two lines above here
        end
    end

    return false
end

现在你可以看到上面的内容没有意义:

  • 你正在混合" e"带有时间戳值的表。
  • 您正在使用setupTable添加的最后一个e的时间戳进行调用,因此valueExists将查找表中的最后一项以获取搜索的时间戳

看一下上面的内容:你在这个valueExists函数中实际尝试做什么?

相关问题