Assign several table values at once with table.unpack.

multiple assignment `table.unpack` spreads selected array values into ordinary variables.

Unpack Window

startIndex
unpack_window.lua
Replay: real traced execution (multi-file project)
local startIndex = 1
local first, second = table.unpack({"north", "east", "south"}, startIndex, startIndex + 1)
print("startIndex=" .. startIndex)
print("first=" .. first)
print("second=" .. second)
local startIndex = 2
local first, second = table.unpack({"north", "east", "south"}, startIndex, startIndex + 1)
print("startIndex=" .. startIndex)
print("first=" .. first)
print("second=" .. second)
  1. startIndex ← 1, first ← north, second ← east

    1local startIndex→ 1 = 1 --@startIndex=22local first→ north, second→ east = table.unpack({"north", "east", "south"}, startIndex1, startIndex + 1)3print("startIndex=" .. startIndex1)4print("first=" .. firstnorth)5print("second=" .. secondeast)
    outputstartIndex=1
    first=north
    second=east
  1. startIndex ← 2, first ← east, second ← south

    1local startIndex→ 2 = 22local first→ east, second→ south = table.unpack({"north", "east", "south"}, startIndex2, startIndex + 1)3print("startIndex=" .. startIndex2)4print("first=" .. firsteast)5print("second=" .. secondsouth)
    outputstartIndex=2
    first=east
    second=south