Computer programs are all about IO - input and output. It's nice to see something come out of a program like in our hello world example. However a real program needs to read some input too. Let's look at a program that does just that.
Open up your IDE and enter the following code in the editor window. Again, don't worry about the meaning of the words and symbols just yet, we will walk through that in a bit. Just type the code in exactly as it appears in the listing.
-- Greeter program.
io.write("Enter your name: ")
local name = io.read()
io.write("Hello " .. name .. "!\n")
Run this program. When prompted to enter your name, type it in using the keyboard and press the enter (return) key. You should see the program say hello to you, like this (where Alice is replaced with the name you enter).
Enter your name: Alice
Hello Alice!Let's breakdown the program line by line.
The first line is a comment. These are for the benefit of you the programmer, or anyone else who happens to read your Lua code. Comments are ignored by the Lua interpreter.
A blank line. Blank lines are ignored by the Lua interpreter. Just like in a book or letter you would have separate paragraphs; you use blank lines to make your code more readable.
A call to the function io.write. This function will print whatever tou tell it to the console as output. Here we have placed a string literal (Enter your name:) in double quotes. This is then placed inside a pair of parenthesis () to pass it as an argument to the io.write function.
Here we are doing two things: introducing a variable called name using the local keyword, and reading some text from the keyboard using the io.read function.
Another call to the io.write function. We are still sending just one argument, but it is a made of several parts. Lets break it down:
"Hello " .. name .. "!\n"
^ ^ ^ ^ ^
| | | | |
| | | | string literal
| | | |
| | | string concatenation operator
| | |
| | variable
| |
| string concatenation operator
|
string literal
We have seen string literals before. They are just text enclosed in double or
single quotes. The .. is an operator that says "glue the things before and after
it together into one string. So here we are concatenating three elements together
to form one string - the literal "hello", the contents of the name variable,
and the literal "!\n" (which is an exclamation mark followed by a new line).
Make the following changes one by one. See if the program still runs and note what is different in the output if anything. If it fails to run, pay attention to any error messages you get.
Remove the double dash -- from the start of the first line.
Remove the word local from the line that reads your name into a variable.
Put double quote marks around the variable name on the last line.
Remove the \n from the last line.
Replace the double quote marks " in the program with single quote marks '.
Change io.write to print in the program.
The first line is a comment, which is ignored by the Lua interpreter. By removing the starting double dash -- the line is no longer treated as a comment, and the program fails to run as the text Greeter program is not a valid Lua command.
The program will run the same as before. By removing the local from the line, the variable name is turned into a global variable. These function just the same, except they can be accessed anywhere in your program. For a small program like this it makes no difference. Using global variables in a larger program can give rise to a number of hard to debug errors in logic. For now, just use local when introducing your variables.
In this line name refers to the variable defined on the line above. By placing it in double quote marks ", it is instead treated as text. Your program would no longer output your name, and uses the literal word "name" instead.
The program no longer prints a new line after the greeting. Remember, the io.write method doesn't print a new line at the end of its output. You have to remember to add it in.
The program still runs fine. This shows you that in Lua, string literals can be enclosed in double " or single ' quotes. As long as the start and end quote match each other.
The program still runs, but it now outputs a new line after each print call. In general it is considered best practice to use io.write for output, and to save print for temporary statements when debugging a problem in your code (more on that later).