Tutorial Examples |
|
If we run the Lua executable without parameters we get the following message and a > command prompt.
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio >
All of the examples in the tutorial are typed at the command prompt > and the output copied and pasted into the wiki.
> print("hello Lua user")
hello Lua user
> print(10*10)
100
If a line begins with an = Lua evaluates the line as an expression. Its shorthand for having to use the print() function.
> = "hello Lua user" hello Lua user > = 10*10 100
We can enter commands over multiple lines if necessary. Lua will try and evaluate what you typed on a given line and assess whether the command is complete. If it is not considered complete a double command prompt appears >> so you can continue typing. E.g.,
> print( >> "Hello lua user" >> ) Hello lua user
print( statement is incomplete as the matching closing bracket is missing. The Lua command line assumes that you have more to type so the >> prompt is displayed. This continues until the statement is deemed complete. If you made a mistake you will get an error. This also works for the evaluation shorthand:
> = 10 * >> 10 + >> 5 105
Where you see "--", followed by text, this is a comment. Everything after the -- is ignored by the Lua compiler on that line.
> = 1+1 -- this is ignored. 2 > = 1+1 no comment stdin:1: <eof> expected near `no'
Note: It is good behaviour to have but you do not have to type the comments in! They are just comments about what is happening for clarity especially for other maintainers to understand(if there are).