Step 1 of 5 · Reading · ~4 min
Learn
First Steps
Hello, Lua!
By the end of this lesson you can write a Lua program that prints exact text to the screen, and you will know the one print behaviour that makes correct-looking output fail a byte-exact check.
Lua was created in 1993 at PUC-Rio in Brazil to solve one problem: give a large program a scripting layer without dragging in a large language. Every other design decision follows from that. The interpreter is a few hundred kilobytes. The standard library is small on purpose. There is exactly one container type. Lua fits in whatever space is left over.
You are running Lua whenever you script a Roblox game, configure Neovim, write a World of Warcraft addon, handle a request in OpenResty, or send EVAL to Redis.
What does a Lua program look like?
A Lua file is a chunk: a list of statements executed top to bottom. There is no main, no class to wrap things in, no import ceremony. The first line of the file is the first line that runs.
print("Welcome aboard")
print("Second line")
--> Welcome aboard
--> Second line
print writes its arguments and then a newline. That newline is automatic; you never append one yourself, and each call starts a new line.
What can you hand to print?
print calls tostring on every argument, so it accepts any value at all.
print(42)
print(3.5)
print(true)
print(nil)
--> 42
--> 3.5
--> true
--> nil
Notice that nil prints as the word nil rather than as nothing. That detail matters later, when a missing value travels quietly through your program instead of crashing it.
Several arguments: the tab rule
print accepts any number of arguments and separates them with a tab character, not a space.
| You write | What lands on stdout |
|---|---|
print("Welcome aboard") | Welcome aboard |
print("Welcome", "aboard") | Welcome + tab + aboard |
On screen a tab and a space look almost identical, so a wrong-separator bug is invisible to the eye. Whenever a grader says your output is wrong and you swear it looks right, check what sits between your words.
The rule cuts both ways. When you want a tab, passing two arguments is the shortest way to get one. Lua strings can also carry it directly through an escape sequence, a backslash plus a letter written inside the quotes:
| Escape | Produces |
|---|---|
\t | a tab |
\n | a line break |
\\ | one backslash |
\" | a double quote |
print("name\tvalue")
print("two\nlines")
--> name value
--> two
--> lines
The gap in the first output line is a TAB, written here as it lands on stdout.
Quotes, comments, semicolons
Single and double quotes are interchangeable: 'hi' and "hi" build the identical string. Pick one style; switch only to avoid escaping.
print('She said "hi"')
--> She said "hi"
Comments start with -- and run to the end of the line. Block comments sit between --[[ and ]].
-- a one-line comment
--[[ a comment that
spans several lines ]]
print("still runs")
Semicolons are legal but optional, and idiomatic Lua leaves them out. A newline ends a statement.
How your work is checked
Every exercise in this course feeds your chunk a fixed input on stdin and compares stdout against an expected string. Capitalisation counts. A tab where a space belongs counts. A blank line in the middle counts, and so does a missing one. What does not count is whitespace trailing off the very end: one extra blank line at the bottom is trimmed before the comparison and still passes. Everything before that has to match, which is why this first page spends so long on the separator between values.
Common mistakes
- Passing the words as separate arguments when you wanted a space.
print("Hello,", "Lua!")writesHello,TABLua!. Join the words into one string instead. - Typing spaces where a tab is required. A line that should hold a tab but holds spaces looks the same and fails. Use
\tinside the string, or two arguments toprint. - Adding your own
\nat the end of aprint.print("a\n")writesa, a newline, and thenprint's own newline: a stray blank line. - Mismatched capitals or punctuation.
hello, lua!is a different string fromHello, Lua!.
Your exercise
Print Two Lines wants exactly two lines of output. The first is the greeting Hello, Lua!. The second is the word Lua, one TAB, and 5.3. The starter is a comment; replace it with your own print calls, one per line of output. The mistake the grader will catch is a space where the tab belongs.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…