in Lua, everything is table, all the data, functions are stored in table, but when we use table, how to clean up the table table data.
first see a function:
table.remove(table[,pos]) : delete the element on pos position, the latter element will move forward one, and then the deleted index will move forward, resulting in the deleted data is not what you want. When pos is not filled in, the data at the end of the table is deleted.
see the following code:
local array = {"a","a","a","b","a","a","b"}
for i,v in ipairs(array) do
if v == "a" then
table.remove(array, i)
end
end
for i,v in ipairs(array) do
print(i,v)
end
output result:
we wanted to delete all “a” data in the table, but the output result did not delete all “a” data. So we want to continuously delete the table data can not be used in this way, here is the correct way to delete.
local array = {"a","a","a","b","a","a","b"}
for i=#array,1,-1 do
if array[i] == "a" then
table.remove(array, i)
end
end
for i,v in ipairs(array) do
print(i,v)
end
so you get the correct deletion.
so we can encapsulate a function
-- 删除table表中符合conditionFunc的数据
-- @param tb 要删除数据的table
-- @param conditionFunc 符合要删除的数据的条件函数
local function table.removeTableData(tb, conditionFunc)
-- body
if tb ~= nil and next(tb) ~= nil then
-- todo
for i = #tb, 1, -1 do
if conditionFunc(tb[i]) then
-- todo
table.remove(tb, i)
end
end
end
end
-- 删除数据是否符合条件
-- @param data 要删除的数据
-- @return 返回是否符合删除data条件
local function removeCondition(data)
-- body
-- TODO data 符合删除的条件
return true
end
we just need to write TODO and pass in the table.
Read More:
- Vector delete pop of element_ back(),erase(),remove()
- Remove the delete button icon from the El upload component
- Prompt 550 remove directory operation failed when FTP delete folder
- Insert into / delete / update / select of SQL Server database table
- Python data cleaning — delete failed images__ Simple version
- MySQL partitions the existing tables of the data table
- Mysql error when deleting the table structure: Error Code: 1217. Cannot delete or update a parent row: a foreign key constraint fails
- (element UI component table) how to add a style to a table
- Docker delete error response from daemon: Conflict: unable to delete xxxxx solution
- Datagrip import & export table structure and data
- [react+antd] Table Error: Unhandled Rejection (TypeError): data.slice is not a function
- MySQL will delete the data minutes before the current time
- Excel pivot table data source not valid
- greendao insert data UNIQUE constraint failed: PURCHASE_ORDER_TABLE._id (code 1555)
- Insufficient table space ORA-00604 unable to extend table SYS.AUD by 8192
- [Solved] Es delete all the data in the index without deleting the index structure, including curl deletion
- Matlab delete row or col to delete the row or column of the matrix
- 1093 – You can’t specify target table ‘table’ for update in FROM clause
- In Python, print() prints to remove line breaks
- Inconsistency between adapter data and UI data after dragging recyclerview (data disorder)