Packing Multiple Results
Lesson 10 of 29 in Coddy's Lua for Luau Developers course.
Multiple returns and varargs work in both languages, including the rule that only the last call in an expression list expands fully. The library names differ: Luau has table.pack and table.unpack, while Lua 5.1 uses global unpack and has no standard pack helper. To preserve nil arguments, store select("#", ...) in a separate count field. A table length is not a reliable argument count when nil creates holes. Pass the saved count as the final unpack bound. The task uses dense numeric input, but a reusable adapter must account for missing values too.
Luau
local args = table.pack(4, nil)
print(select("#", table.unpack(args, 1, args.n)))Lua 5.1
local function pack(...) return {n=select("#", ...), ...} end
local args = pack(4, nil)
print(select("#", unpack(args, 1, args.n)))Porting trap
Do not copy a table.pack call unchanged or replace its count field with the length operator. A trailing nil still occupies a result position.
Challenge
EasyInput is a sequence of integer lines, possibly empty. Define bounds(...) using varargs. Return minimum and maximum separated by a colon, or empty when no values are supplied.
Edit app.lua. The visible locked main.lua reads stdin into a sequence of lines, calls app.run(lines), and prints its returned value with one final newline. Do not print diagnostics or change the module names. Empty input gives an empty sequence. All numeric inputs are small enough to avoid overflow.
Try it yourself
local app = require("app")
local lines = {}
for line in io.lines() do lines[#lines + 1] = line end
print(app.run(lines))
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Lua for Luau Developers
2Flow and Functions
Generalized IterationReplacing continueErasing Type SyntaxPacking Multiple ResultsPractice on your own: Online Lua compiler