簡體   English   中英

lua array2d檢查值是否存在

[英]lua array2d check value if exist

我真的需要幫助。 我仍然無法弄清楚我的邏輯lua代碼,關於如何在條件值已經存在的情況下使用if條件阻止事件?

這是我的array2d:

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

實際上,我的計划是獲取正確的代碼來檢查索引名稱mydata[..][1]和點索引mydata[..][3]

這是一個示例案例說明:


因此,如果索引名Michael和點指數3不存在, 或者 如果只命名michael存在,但它不具有點指數3 ,那么它會寫michael:50:3data.txt。 如果名稱和點索引已經存在。 然后,它將阻止並打印(“抱歉,您的姓名和分數索引已經存在”)

我的代碼:

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

當事件“ write michael:50:3 ”已經存在時,阻止該事件的最佳方法是什么? 讓我知道在幫助我之前是否需要了解一些內容。 我的lua仍然很差。

我們可以將其分解為一個通用的比較函數,該函數在一組記錄中搜索與提供的(部分或全部)匹配的存儲記錄。

我們必須定義的第一個函數將是一個輔助函數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

然后,我們可以輕松實現搜索功能,該功能搜索一記錄,並針對同一query記錄檢查每個記錄。 我們將其稱為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

現在,我們可以簡單地使用它們來查找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

由於我們存儲的記錄是序列,因此我們使用顯式括號符號設置索引,在這種情況下,跳過第二個索引。

由於此函數在發生匹配的情況下返回一個表,而在沒有匹配的情況下返回nil ,因此我們現在有了一個安全的布爾構造函數; 表是一個真實的值,而nil是一個虛假的值。

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

返回記錄使我們更容易進行進一步的檢查,而無需再次查詢每條記錄。

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