util.table.insert
Shared
Inserts an element into a table at a specified position
Syntax
util.table.insert(
input,
value,
index = util.table.len(input) + 1
)Parameters
| Type | Name | Description |
|---|---|---|
table | input | Table to insert into |
any | value | Value to insert |
int | index | Index where the element should be inserted |
Returns
| Type | Name | Description |
|---|
Examples
local t = {1, 2, 3}
util.table.insert(t, 4)
core.engine.print("info", util.table.concat(t, ", ")) -- '1, 2, 3, 4'local t = {"a", "c", "d"}
util.table.insert(t, 2, "b")
core.engine.print("info", util.table.concat(t, ", ")) -- 'a, b, c, d'local t = {2, 3, 4}
util.table.insert(t, 1, 1)
core.engine.print("info", util.table.concat(t, ", ")) -- '1, 2, 3, 4'local numbers = {}
for i = 1, 5 do
util.table.insert(numbers, i*10)
end
core.engine.print("info", util.table.concat(numbers, ", ")) -- '10, 20, 30, 40, 50'local words = {"hello"}
util.table.insert(words, "world")
util.table.insert(words, "lua")
core.engine.print("info", util.table.concat(words, " ")) -- 'hello world lua'local colors = {"red", "green", "blue"}
util.table.insert(colors, 2, "yellow")
core.engine.print("info", util.table.concat(colors, ", ")) -- 'red, yellow, green, blue'local t = {}
util.table.insert(t, "first")
core.engine.print("info", t[1]) -- 'first'local stack = {}
util.table.insert(stack, "item1")
util.table.insert(stack, "item2")
util.table.insert(stack, "item3")
core.engine.print("info", #stack) -- 3local names = {}
local inputs = {"Alice", "Bob", "Charlie"}
for _, name in ipairs(inputs) do
util.table.insert(names, name)
endlocal lists = {{1, 2}, {3, 4}}
util.table.insert(lists, {5, 6})
core.engine.print("info", #lists) -- 3