Lua中對自定義二維表進行新增、修改、計算、刪除、判斷是否存在操作
阿新 • • 發佈:2022-05-26
引言:
最近剛稍微深入瞭解一下Lua,正好最近需要用到Lua中對錶的操作,於是藉助現有的瞭解實現了對一個簡單的二維表進行新增、修改、計算、刪除及判斷存在的操作
表的建立及相關方法:
1. 建立表及自定義一個迭代器:
--1.當作這是一個操作類/表;該表為一個二維列表,每一個元素代表一個表 --2.每一個元素(表)中包含 id 和 num operationList = {} --1.列表元素迭代器,僅返回列表中每一個元素,改列表索引必須為連續的數字 function listIterator(list) local index = 0; local listLen = #listreturn function () index = index + 1 if index <= listLen then return list[index] end end end
2. 向表中新增資料
--1.向操作類/表中新增資料,oId代表標識ID,onum代表值當前值 --2.當 oId 已在列表中存在時,不進行新增操作 --2.當 oId 為nil時,預設為 “1”,當 onum 為nil時,預設為 1 --3.oId 型別為string,oNum 型別為number --4.新增成功返回 true,失敗則返回 falsefunction operationList:add(oId, oNum) if not operationList:exist(oId) then table.insert(self, {id = oId or "1", num = oNum or 1}) return true end return false end
3. 修改表中資料
--1.修改operationList指定 ID 的值 --2.oId為指定ID,oNum為修改值,ifNum為指定ID的指定值,oCreate為是否新建值 --2.oId 型別為string,oNum 型別為number,ifNum型別為number,oCreate型別為boolean--3.當該 ID 不存在時且引數 oCreate = true,則建立 --4.若當前 ID 存在, oNum 為 nil,ifNum 不存在,返回當前 ID 的 num 值 --5.若當前 ID 存在, oNum 不為 nil,ifNum 不存在,則為修改後的 num 值 --6.若當前 ID 存在, oNum 不為 nil,ifNum 存在,則為修改後的 num 值 --7.若當前 ID 存在, oNum 不為 nil,ifNum 不存在,返回當前 ID 的 num 值 --8.若當前 ID 不存在,且 oCreate = false 或 = nil,則返回數字 -1 --9.若當前 ID 不存在,且 ocreate = true,則建立對應 ID 和 num = 1 的新子列,再返回數字 0 function operationList:alter(oId, oNum, ifNum, oCreate) if not ifNum then for ol in listIterator(operationList) do if ol.id == oId then --若與要修改的ID相同 if oNum then --若修改值不為nil ol.num = oNum --修改值 end return ol.num --返回當前值,若無修改值,則返回原來的值 end end else for ol in listIterator(operationList) do if ol.id == oId then --若與要修改的ID相同且 該ID的值 = 判定值 if ol.num == ifNum then ol.num = oNum --修改值 end return ol.num --返回當前值,若原來的值不等於ifNum的值,則返回原來的值 end end end if not oCreate then --若列表不存在 oId,且 oCreate的值為false return -1 --返回 -1 end operationList:add(oId, 1) --若列表不存在 oId,且 oCreate的值為true return 0 --返回 0 end
4. 計算表中值資料
--1.計算operationList指定 ID 的值 --2.oId為指定ID,computeNum為計算值,ifNum為指定ID的指定值,oCreate為是否新建值 --2.oId 型別為string,computeNum 型別為number,ifNum型別為number,oCreate型別為boolean --3.當該 ID 不存在時且引數 oCreate = true,則建立 --4.若當前 ID 存在, computeNum 為 nil,ifNum 不存在,返回當前 ID 的 num + 1 值 --5.若當前 ID 存在, computeNum 不為 nil,ifNum 不存在,則為修改後的 num + computeNum 值 --6.若當前 ID 存在, computeNum 不為 nil,ifNum 存在,則為修改後的 num + computeNum 值 --7.若當前 ID 存在, computeNum 不為 nil,ifNum 不存在,則返回當前 ID 的 num 值 --8.若當前 ID 不存在,且 oCreate = false 或 = nil,則返回數字 -1 --9.若當前 ID 不存在,且 ocreate = true,則建立對應 ID 和 num = 1 的新子列,再返回數字 0 function operationList:compute(oId, computeNum, ifNum, oCreate) if not ifNum then --如果相等判定值不存在 for ol in listIterator(operationList) do if ol.id == oId then if not computeNum then --如果計算值為空,那麼原值預設 + 1 ol.num = ol.num + 1 else ol.num = ol.num + computeNum --計算值不為空,那麼原值 + 計算值 end return ol.num end end else for ol in listIterator(operationList) do if ol.id == oId then if ol.num == ifNum and computeNum then ol.num = ol.num + computeNum end return ol.num end end end if not oCreate then --若列表不存在 oId,且 oCreate的值為false return -1 --返回 -1 end operationList:add(oId, 1) --若列表不存在 oId,且 oCreate的值為true return 0 endView Code
5. 刪除指定ID的資料
--1.從表移除指定 ID 的子列 --2.oId 型別為string --3.移除成功返回 true,失敗返回 false(只有當oId不存在時才會失敗) function operationList:remove(oId) local removeIndex for olI, olV in pairs(operationList) do if olV.id == oId then --如果找到ID,則將對應索引傳遞給removeIndex removeIndex = olI break end end if not removeIndex then return false end table.remove(operationList, removeIndex) return true end
6. 判斷指定ID是否存在表中
--1.判斷指定 ID 在operationList中是否存在 --2.oId 型別為string function operationList:exist(oId) for ol in listIterator(operationList) do if ol.id == oId then return true end end return false end
對以上方法進行測試:
1. 測試程式碼:
--下面是向表operation中插入資料及輸出結果 operationList:add("test1", 100) operationList:add("test2", 200) operationList:add("test3", 300) operationList:add("test4") operationList:add(nil, 500) --下面是操作表 operationList:remove("test3") --移除資料 print(operationList:exist("11")) --判斷 ID = 11 的子列是否存在,輸出false print(operationList:exist("1")) --判斷 ID = 1 的子列是否存在,輸出true print(tostring(operationList:alter("test3"))) --通過修改值方法向列表新增新值,輸出 -1 print(tostring(operationList:alter("test3", nil, nil, true))) --通過修改值方法向列表新增新值,輸出 0 print(tostring(operationList:alter("1"))) --獲取 ID = 1 的值,因為存在,輸出當前 ID 的值 print(tostring(operationList:alter("11"))) --獲取 ID = 11 的值,因為不存在,輸出 -1 print(tostring(operationList:alter("test1", 99))) --修改 ID = test1 的值為 99,因為存在,輸出修改後的值 print(tostring(operationList:alter("11", 99))) --修改 ID = 11 的值為 99,因為不存在,輸出 -1 print(tostring(operationList:alter("test1", 999, 100))) --修改 ID = test1 且當前值 = 100 的值為 999,因為ID存在 但 值判斷值不相同,輸出當前值 print(tostring(operationList:alter("test1", 999, 99))) --修改 ID = test1 且當前值 = 99 的值為 999,因為ID存在 且 值判斷值相同,輸出修改後的值 print(tostring(operationList:alter("11", 666, 999))) --修改 ID = 11 且當前值 = 999 的值為 666,因為ID不存在,輸出 -1 print(tostring(operationList:compute("1"))) --計算 ID = 1 的值,其他引數為空,獲得原值 + 1 print(tostring(operationList:compute("1", -2))) --計算 ID = 1 的值,計算值 = -2,獲得原值 - 2 print(tostring(operationList:compute("1", -2, 499))) --計算 ID = 1 的值,計算值 = -2,且當前值 = 499 獲得原值 - 2 print(tostring(operationList:compute("1", nil, 497))) --計算 ID = 1 的值,計算值 = nil,且當前值 = 497 獲得原值 print(tostring(operationList:compute("1", -2, 499))) --計算 ID = 1 的值,計算值 = -2,但當前值 ~= 499 獲得原值 print(tostring(operationList:compute("test6", -2, 499))) --計算 ID = test6 的值,計算值 = -2,但當前值 ~= 499 且 oCreate = nil ,不建立新子列,獲得返回值 -1 print(tostring(operationList:compute("test6", -2, 499, true))) --計算 ID = test6 的值,計算值 = -2,但當前值 ~= 499 且 oCreate = true 建立新子列,獲得返回值 0 --輸出列表 print("\n") for thisoL in listIterator(operationList) do print("this ID is "..thisoL.id.."\nthis Num is "..thisoL.num.."\n") end
2. 測試結果圖:
以上就是全部內容了