table.unpack returns sequence values as separate results.

split into names Unpacking assigns positions from a table to separate local variables.

Unpack Values

secondValue
unpack_values.lua
Replay: real traced execution (multi-file project)
local secondValue = "right"
local first, second = table.unpack({"left", secondValue})
local summary = first .. ":" .. second

print("secondValue=" .. secondValue)
print("summary=" .. summary)
local secondValue = "center"
local first, second = table.unpack({"left", secondValue})
local summary = first .. ":" .. second

print("secondValue=" .. secondValue)
print("summary=" .. summary)
local secondValue = "edge"
local first, second = table.unpack({"left", secondValue})
local summary = first .. ":" .. second

print("secondValue=" .. secondValue)
print("summary=" .. summary)
  1. secondValue ← right, first ← left, second ← right, summary ← left:right

    1local secondValue→ right = "right" --@secondValue="center", "edge"2local first→ left, second→ right = table.unpack({"left", secondValueright})3local summary→ left:right = firstleft .. ":" .. secondright45print("secondValue=" .. secondValueright)6print("summary=" .. summaryleft:right)
    outputsecondValue=right
    summary=left:right
  1. secondValue ← center, first ← left, second ← center, summary ← left:center

    1local secondValue→ center = "center"2local first→ left, second→ center = table.unpack({"left", secondValuecenter})3local summary→ left:center = firstleft .. ":" .. secondcenter45print("secondValue=" .. secondValuecenter)6print("summary=" .. summaryleft:center)
    outputsecondValue=center
    summary=left:center
  1. secondValue ← edge, first ← left, second ← edge, summary ← left:edge

    1local secondValue→ edge = "edge"2local first→ left, second→ edge = table.unpack({"left", secondValueedge})3local summary→ left:edge = firstleft .. ":" .. secondedge45print("secondValue=" .. secondValueedge)6print("summary=" .. summaryleft:edge)
    outputsecondValue=edge
    summary=left:edge

Split Into Names

  1. The table holds left and right.
  2. table.unpack returns both values.
  3. first receives left.
  4. second receives right.
  5. The summary joins them as left:right. | Returned position | Variable | Value | | --- | --- | --- | | 1 | first | left | | 2 | second | right |

Exercise: unpack_values.lua

Unpack two table values into names, join them with a colon, and print the summary