move()

Published Oct 3, 2023
Contribute to Docs

The .move() function copies the specified indices’ values from the source table into the provided starting index of the destination table.

Syntax

table.move(a1, f, e, t, a2)
  • .move() function expects five arguments where a1 is the source table and a2 is the destination table.
  • f means from (First index within a1).
  • e means end (Last index within a1).
  • t means to (First index within a2).

Example

Consider the following tables:

local Table1 = {"šŸŽ", "šŸŒ", "šŸ‡", "šŸ“", "šŸ‰" }
local Table2 = {"šŸŽ", "šŸŒ"}
table.move(Table1, 1, #Table1, 3, Table2)
print(table.unpack(Table2))

In this example, the elements in Table1 have been copied to Table2 using the .move() function. Starting from index 1 in Table1 until the end and then copying them in sequence to Table2 starting from index 3.

The above code will result in:

šŸŽšŸŒšŸŽšŸŒšŸ‡šŸ“šŸ‰

Note: In Lua, indices start from 1; thus, the next available index in Table2 is index 3.

All contributors

Looking to contribute?

Learn Lua on Codecademy