util.table.move

Shared

Moves elements from one table to another


Syntax

local result = util.table.move(
    from,
    start_at,
    end_at,
    move_at,
    to = from
)

Parameters

TypeNameDescription
tablefromTable to copy from
intstart_atPosition where moving starts
intend_atPosition where moving ends
intmove_atPosition where its moving at
tabletoTable to move into

Returns

TypeNameDescription
tableresultAltered destination table

Examples

Copy elements to another table
local src = {1, 2, 3, 4, 5}
local dest = {}
util.table.move(src, 1, 3, 1, dest)

core.engine.print("info", util.table.concat(dest, ", ")) -- '1, 2, 3'
Move elements within the same table
local t = {10, 20, 30, 40, 50}
util.table.move(t, 1, 2, 4)

core.engine.print("info", util.table.concat(t, ", ")) -- '10, 20, 30, 10, 20'
Shift elements right within a table
local t = {1, 2, 3, 4}
util.table.move(t, 1, 4, 2)

core.engine.print("info", util.table.concat(t, ", ")) -- '1, 1, 2, 3, 4'
Copy a subset of elements to a new position
local t = {"a", "b", "c", "d", "e"}
util.table.move(t, 2, 4, 6)

core.engine.print("info", util.table.concat(t, ", ")) -- 'a, b, c, d, e, b, c, d'
Clone an entire table using move
local original = {1, 2, 3}
local clone = {}
util.table.move(original, 1, util.table.len(original), 1, clone)

core.engine.print("info", util.table.concat(clone, ", ")) -- '1, 2, 3'
Insert at the beginning by shifting right
local t = {2, 3, 4}
util.table.move(t, 1, util.table.len(t), 2)
t[1] = 1

core.engine.print("info", util.table.concat(t, ", ")) -- '1, 2, 3, 4'
Append elements from another table
local t1 = {1, 2, 3}
local t2 = {4, 5, 6}
util.table.move(t2, 1, util.table.len(t2), util.table.len(t1) + 1, t1)

core.engine.print("info", util.table.concat(t1, ", ")) -- '1, 2, 3, 4, 5, 6'
Remove an element by shifting left
local t = {1, 2, 3, 4, 5}
util.table.move(t, 3, 5, 2)
util.table.remove(t)
util.table.remove(t)

core.engine.print("info", util.table.concat(t, ", ")) -- '1, 3, 4, 5'
Duplicate a range of elements
local t = {"x", "y", "z"}
util.table.move(t, 1, 3, 4)

core.engine.print("info", util.table.concat(t, ", ")) -- 'x, y, z, x, y, z'
Copy a partial range into a destination table
local src = {10, 20, 30, 40, 50}
local dest = {0, 0, 0, 0, 0}
util.table.move(src, 2, 4, 1, dest)

core.engine.print("info", util.table.concat(dest, ", ")) -- '20, 30, 40, 0, 0'
Copy elements in reverse order
local src = {1, 2, 3, 4}
local dest = {}
for i = util.table.len(src), 1, -1 do
    util.table.move(src, i, i, util.table.len(src) - i + 1, dest)
end

core.engine.print("info", util.table.concat(dest, ", ")) -- '4, 3, 2, 1'
Efficiently extend an array with another
local base = {1, 2, 3}
local extension = {4, 5, 6, 7, 8}
util.table.move(extension, 1, util.table.len(extension), util.table.len(base) + 1, base)

core.engine.print("info", util.table.concat(base, ", ")) -- '1, 2, 3, 4, 5, 6, 7, 8'

On this page