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