Lua move()

EbrahimAA's avatar
Published Oct 3, 2023

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

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Lua, a general-purpose programming language used for building games, web apps, and developer tools.
    • Beginner Friendly.
      4 hours

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

Learn Lua on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Lua, a general-purpose programming language used for building games, web apps, and developer tools.
    • Beginner Friendly.
      4 hours