util.table.remove
Shared
Removes and returns an element from a table
Syntax
local value = util.table.remove(
input,
position = util.table.len(input)
)Parameters
| Type | Name | Description |
|---|---|---|
table | input | Table to remove from |
int | position | Index of element to remove |
Returns
| Type | Name | Description |
|---|---|---|
any | value | Removed element on successful execution, or nil if index is empty |
Examples
local t = {1, 2, 3, 4}
local removed = util.table.remove(t)
core.engine.print("info", removed) -- 4
core.engine.print("info", util.table.concat(t, ", ")) -- 1, 2, 3local t = {"a", "b", "c", "d"}
local removed = util.table.remove(t, 2)
core.engine.print("info", removed) -- b
core.engine.print("info", util.table.concat(t, ", ")) -- 'a, c, d'local t = {10, 20, 30}
local removed = util.table.remove(t, 1)
core.engine.print("info", removed) -- 10
core.engine.print("info", util.table.concat(t, ", ")) -- '20, 30'local stack = {"first", "second", "third"}
local top = util.table.remove(stack)
core.engine.print("info", top) -- 'third'
core.engine.print("info", #stack) -- 2local queue = {"task1", "task2", "task3"}
local next = util.table.remove(queue, 1)
core.engine.print("info", next) -- 'task1'
core.engine.print("info", util.table.concat(queue, ", ")) -- 'task2, task3'-- Remove all elements
local t = {1, 2, 3}
while util.table.len(t) > 0 do
local value = util.table.remove(t)
core.engine.print("info", value)
end
--[[
Output:
3
2
1
]]local t = {}
local removed = util.table.remove(t)
core.engine.print("info", removed) -- nillocal colors = {"red", "green", "blue", "yellow"}
util.table.remove(colors, 3)
core.engine.print("info", util.table.concat(colors, ", ")) -- 'red, green, yellow'local tasks = {"a", "b", "c"}
while #tasks > 0 do
local task = util.table.remove(tasks, 1)
core.engine.print("info", "Processing:", task)
end