Interpolation to Text
Lesson 2 of 29 in Coddy's Lua for Luau Developers course.
Luau backtick strings can evaluate expressions inside braces. Lua 5.1 does not parse that syntax. Build the same text with .. or string.format. The familiar quoted strings, long strings and -- comments still work. Both runtimes separate multiple arguments to print with tabs, so assemble one argument when the exact spacing matters. Keep the template punctuation inside string literals; concatenation does not insert separators. In the task, your module returns a greeting and the locked driver prints it. Returning a value and printing it are different responsibilities.
Luau
local name = "Ada"
print(`Hi, {name}!`)Lua 5.1
local name = "Ada"
print("Hi, " .. name .. "!")Porting trap
Remove interpolation syntax as well as type annotations. Merely changing the filename does not make a Luau program valid Lua.
Challenge
EasyReturn Hello, followed by the first input line exactly, or an empty name when input is absent. Spaces and punctuation are significant.
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
1Syntax and Runtime
Run Lua ModulesInterpolation to TextFloor Division and NumbersBytes and UTF-8 LibrariesFalse Values and DefaultsConditional ExpressionsPractice on your own: Online Lua compiler