How to delete qualified data in the table using table.remove in tolua

How to delete qualified data in the table using table.remove in tolua

  • introduce
  • Problem (wrong way to delete data)
  • Correct deletion plan
    • Delete from back to front
    • Delete recursively
    • Insert new table method
  • Expand it
  • Summarize

Introduction

Deleting qualified data in a table in Lua is actually very simple, but there is a sequence problem, because data deletion in Lua tables needs to be deleted through table.remove. When you delete the previous one, the index value changes.

Problem (wrong way to delete data)

--Test lua table
local tab1 = {<!-- -->
        [1] = {<!-- -->
            Id = 101,
            value1 = 1,
            value2 = 2,
            value3 = 3,
        },
        [2] = {<!-- -->
            Id = 105,
            value1 = 1,
            value2 = 2,
            value3 = 3,
        },
        [3] = {<!-- -->
            Id = 101,
            value1 = 1,
            value2 = 2,
            value3 = 3,
        },
        [4] = {<!-- -->
            Id = 108,
            value1 = 1,
            value2 = 2,
            value3 = 3,
        },
        [5] = {<!-- -->
            Id = 101,
            value1 = 1,
            value2 = 2,
            value3 = 3,
        },
    }
\t
--Wrong way one
for k, v in pairs(tab1) do
        if(v.Id == 101) then
        table.remove(table,k)
        end
    end
    --Same as the deletion method above (with a different writing method)
    for i = 1, #tab1 do
    if(tab1[i].Id == 101) then
        table.remove(table,i)
        end
end
--Wrong way two
local index = 1
for i = 1, #tab1 do
    if(tab1[i].Id == 101) then
        table.remove(table,index)
        index = index - 1
        end
        index = index + 1
end

The above two methods are wrong, and the final printing is not the actual printing as imagined

Correct deletion plan

Delete from back to front

--Lua table of test data
    local tab1 = {<!-- -->
        [1] = {<!-- -->
            Id = 101,
            value1 = 1,
            value2 = 2,
            value3 = 3,
        },
        [2] = {<!-- -->
            Id = 105,
            value1 = 1,
            value2 = 2,
            value3 = 3,
        },
        [3] = {<!-- -->
            Id = 101,
            value1 = 1,
            value2 = 2,
            value3 = 3,
        },
        [4] = {<!-- -->
            Id = 108,
            value1 = 1,
            value2 = 2,
            value3 = 3,
        },
        [5] = {<!-- -->
            Id = 101,
            value1 = 1,
            value2 = 2,
            value3 = 3,
        },
    }
    
this.RemoveTabValue(tab1,101)
    
    for k, v in pairs(tab1) do
        logError("k ========>"..tostring(k))
        logError("v.Id ========>"..v.Id)
    end
    
function this.RemoveTabValue(tab,Id)
    for i = #tab, 1 ,-1 do
        if tab[i].Id == Id then
            table.remove(tab,i)
        end
    end
end

Print as follows

Recursive deletion

--Lua table of test data
    local tab1 = {<!-- -->
        [1] = {<!-- -->
            Id = 101,
            value1 = 1,
            value2 = 2,
            value3 = 3,
        },
        [2] = {<!-- -->
            Id = 105,
            value1 = 1,
            value2 = 2,
            value3 = 3,
        },
        [3] = {<!-- -->
            Id = 101,
            value1 = 1,
            value2 = 2,
            value3 = 3,
        },
        [4] = {<!-- -->
            Id = 108,
            value1 = 1,
            value2 = 2,
            value3 = 3,
        },
        [5] = {<!-- -->
            Id = 101,
            value1 = 1,
            value2 = 2,
            value3 = 3,
        },
    }
    
this.RemoveTabValue(tab1,101)
    
    for k, v in pairs(tab1) do
        logError("k ========>"..tostring(k))
        logError("v.Id ========>"..v.Id)
    end

--recursive method
function this.RemoveTabValue(tab,Id)
    for k, v in pairs(tab) do
        if v.Id == Id then
            table.remove(tab,k)
            this.RemoveTabValue(tab,Id)
            break
        end
    end
end

Print as follows

Insert new table method

--Lua table of test data
    local tab1 = {<!-- -->
        [1] = {<!-- -->
            Id = 101,
            value1 = 1,
            value2 = 2,
            value3 = 3,
        },
        [2] = {<!-- -->
            Id = 105,
            value1 = 1,
            value2 = 2,
            value3 = 3,
        },
        [3] = {<!-- -->
            Id = 101,
            value1 = 1,
            value2 = 2,
            value3 = 3,
        },
        [4] = {<!-- -->
            Id = 108,
            value1 = 1,
            value2 = 2,
            value3 = 3,
        },
        [5] = {<!-- -->
            Id = 101,
            value1 = 1,
            value2 = 2,
            value3 = 3,
        },
    }
    
local newtab = {<!-- -->}
\t
    for k, v in pairs(tab1) do
        if v.Id == 101 then
            table.insert(newtab, v)
        end
    end
    --I have not written a method to delete the tab1 table here, which means it still occupies memory, so it is equivalent to opening up a new memory space.
    --You can delete the data of the original tab1 table by yourself, or use the above two methods
    --This method takes up additional memory space
    
    for k, v in pairs(newtab) do
        logError("k ========>"..tostring(k))
        logError("v.Id ========>"..v.Id)
    end

Print as follows

Expand it

To briefly talk about the knowledge here, if you encounter the following dictionary type Lua table

  1. The length result of #tab1 is 3, not 5, and [true] and [“a”] are excluded (key-value pairs that are not numbers k are not recognized)
  2. All key-value pairs can only be read using pairs. If ipairs is used, only [1][2][3] key-value pairs with number k can be read.
local tab1 = {<!-- -->
        [1] = {<!-- -->
            Id = 101,
            value1 = 1,
            value2 = 2,
            value3 = 3,
        },
        [2] = {<!-- -->
            Id = 105,
            value1 = 1,
            value2 = 2,
            value3 = 3,
        },
        [3] = {<!-- -->
            Id = 101,
            value1 = 1,
            value2 = 2,
            value3 = 3,
        },
        [true] = {<!-- -->
            Id = 108,
            value1 = 1,
            value2 = 2,
            value3 = 3,
        },
        ["a"] = {<!-- -->
            Id = 101,
            value1 = 1,
            value2 = 2,
            value3 = 3,
        },
    }

Summary

If the article is helpful to you, you can leave free love and attention, thank you