简体   繁体   English

lua array2d检查值是否存在

[英]lua array2d check value if exist

i really need help. 我真的需要帮助。 i am still can't figure out my logic lua code about How can i block the event with an if condition, when it already has a value? 我仍然无法弄清楚我的逻辑lua代码,关于如何在条件值已经存在的情况下使用if条件阻止事件?

this is my array2d : 这是我的array2d:

mydata = {
{"mike", "30", "1"},
{"michael", "40", "2"},
{"mike", "40", "2"},
{"michael", "50", "3"},
{"frost", "50", "3"},
{"nick", "60", "4"}
}

actually, my plan is to get the correct code to check the index name mydata[..][1] and point index mydata[..][3] . 实际上,我的计划是获取正确的代码来检查索引名称mydata[..][1]和点索引mydata[..][3]

This is an example case explanation : 这是一个示例案例说明:


so if the index name Michael and point index 3 are not exist or if only name michael exist but it doesn't have point index 3 , then it will write michael:50:3 in the data.txt . 因此,如果索引名Michael和点指数3不存在, 或者 如果只命名michael存在,但它不具有点指数3 ,那么它会写michael:50:3data.txt。 but if name and point index already exist. 如果名称和点索引已经存在。 Then, it will block and print("sorry your name and score index already exist") 然后,它将阻止并打印(“抱歉,您的姓名和分数索引已经存在”)

My Code : 我的代码:

mydata = {{"mike", "30", "1"},
            {"michael", "40", "2"},
            {"mike", "40", "2"},
            {"michael", "50", "3"},
            {"frost", "50", "3"},
            {"nick", "60", "4"}}

function check_point1(tab, val1) -- Function for checking name
    for index, value in ipairs (tab) do
        if value[1] == val1 then
            return true
        end
    end
    return false
end

function check_point2(tab, val1) -- Function for checking player point after name
        for index, value in ipairs (tab) do
            if value[3] == val1 then
                return true
            end
        end
        return false
    end

-- this is example case
-- these below are variable to process checking if it's exist
name = "michael"
id = "3"

-- this is my checking code, idk it's good or not. or maybe it's wrong code. because it's not working
if check_point1(mydata, name) then
  if check_point2(mydata, id) then
    print(your name and point index are exist. sorry you can't earn this point")
  else
    print(only your name exist. let us save your new point now")
  end
else
  print("your name and point index is not exist. let us save your name and point now")
  local file = io.open("data.txt", "r")
  file:write("michael:50:3")
  file:close()
end

What would be the best way for blocking an event "write michael:50:3 " when it's already exists ? 当事件“ write michael:50:3 ”已经存在时,阻止该事件的最佳方法是什么? let me know if there some thing you need to understand before you help me out. 让我知道在帮助我之前是否需要了解一些内容。 i am still poor at lua. 我的lua仍然很差。

We can break this down into a generalized comparison function, one which searches a set of records for a stored record which matches a provided one (partial or full). 我们可以将其分解为一个通用的比较函数,该函数在一组记录中搜索与提供的(部分或全部)匹配的存储记录。

The first function we'd have to define would be a helper function, contains_all , which takes a target record, and a query record, and tests to see if the target contains at least everything the query does. 我们必须定义的第一个函数将是一个辅助函数contains_all ,它接受一个target记录和一个query记录,并进行测试以查看target是否至少包含query所做的一切。

local function contains_all (target, query)
    for key, value in pairs(query) do
        if target[key] ~= value then
            return false
        end
    end

    return true
end

We can then easily implement our search function, which searches through a set of records, and checks each record against the same query record. 然后,我们可以轻松实现搜索功能,该功能搜索一记录,并针对同一query记录检查每个记录。 We call this function find_by because in the event that it finds a matching record, we return that record, and the index it was found at. 我们将其称为find_by函数,因为在找到匹配记录的情况下,我们将返回该记录以及在其处找到的索引。

local function find_by (set, query)
    for i = 1, #set do
        if contains_all(set[i], query) then
            return set[i], i
        end
    end
end

Now we can simply use these to find out if mydata contains a record which matches or partially matches our query record of choice. 现在,我们可以简单地使用它们来查找mydata包含匹配或部分匹配我们选择的query记录的记录。

local mydata = {
    {"mike", "30", "1" },
    { "michael", "40", "2" },
    { "mike", "40", "2" },
    { "michael", "50", "3" },
    { "frost", "50", "3" },
    { "nick", "60", "4" }
}

print(find_by(mydata, { [1] = "michael", [3] = "3" })) --> table: 0x217d890 4

Since our stored records are sequences, we use explicit bracket notation to set our indices, skipping the second index in this case. 由于我们存储的记录是序列,因此我们使用显式括号符号设置索引,在这种情况下,跳过第二个索引。

Since this function returns a table in the event of a match, and nil in the event of no match, we now have a safe boolean constrast; 由于此函数在发生匹配的情况下返回一个表,而在没有匹配的情况下返回nil ,因此我们现在有了一个安全的布尔构造函数; tables are a truthy value, and nil is a falsy one. 表是一个真实的值,而nil是一个虚假的值。

if find_by(mydata, { [1] = "michael", [3] = "3" }) then
    print('Entry exists')
end

Returning the record makes it easier for us to make further checks, without querying every record again. 返回记录使我们更容易进行进一步的检查,而无需再次查询每条记录。

local found = find_by(mydata, { "michael" })

if found then
    if found[3] == "3" then
        print('Entry exists.')
    else
        print('Only name exists.')
    end
else
    print('Entry does not exist')
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM