Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function removeByKey(tab, val)
- local found = false
- -- Iterate through the table in reverse order
- for i = #tab, 1, -1 do
- if tab[i] == val then
- table.remove(tab, i)
- -- Remove the element at index i
- found = true
- end
- end
- -- If any elements were removed, create a new table with reset indices
- if found then
- local newTable = {}
- for i, v in ipairs(tab) do
- newTable[i] = v
- end
- return newTable
- else
- return tab
- -- Value not found, return the original table
- end
- end
- function removeAndRebuild(tab, index)
- -- Check if the index is valid
- if index < 1 or index > #tab then
- print("Invalid index")
- return tab
- end
- -- Remove the element at the specified index
- table.remove(tab, index)
- -- Rebuild the table without nil values
- local newTable = {}
- for i, v in ipairs(tab) do
- newTable[#newTable + 1] = v
- end
- return newTable
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement