English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Lua is very easy to learn, and we can create the first Lua program!
Lua provides an interactive programming mode. We can input the program in the command line and see the effect immediately.
The Lua interactive programming mode can be enabled through the command lua -i or lua to enable:
$ lua -i $ Lua 5.3.0 Copyright (C) 1994-2015 Lua.org, PUC-Rio >
In the command line, enter the following command:
> print("Hello World!")
Then we press the Enter key, and the output will be as follows:
> print("Hello World!") Hello World! >
We can save the Lua program code in a file ending with .lua and execute it, which is called script programming. For example, we store the following code in a script file named hello.lua:
print("Hello World!") print("www.oldtoolbag.com)
Execute the script using the lua command, and the output will be:
$ lua hello.lua Hello World! www.oldtoolbag.com
We can also modify the code as follows to execute the script (add #! at the beginning):/usr/local/bin/lua):
#!/usr/local/bin/lua print("Hello World!") print("www.oldtoolbag.com)
In the above code, we specify the Lua interpreter /usr/local/bin directory. Adding the # symbol marks it as a comment for the interpreter. Next, we add executable permissions to the script and execute it:
./hello.lua Hello World! www.oldtoolbag.com
Two dashes are used for single-line comments:
--
--[[ Multiline comments Multiline comments --]]
Lua identifiers are used to define a variable, function, or access other user-defined items. Identifiers start with a letter A to Z or a to z or an underscore _ followed by zero or more letters, underscores, or digits (0 to 9)。
It is best not to use identifiers that start with an underscore followed by uppercase letters, because Lua's reserved words are also like this.
Lua does not allow the use of special characters such as @, $, and % to define identifiers. Lua is a case-sensitive programming language. Therefore, in Lua, w3codebox and w3codebox is two different identifiers. The following lists some correct identifiers:
mohd zara abc move_name a_123 myname50 _temp j a23b9 retVal
The following lists Lua's reserved keywords. Reserved keywords cannot be used as constants, variables, or other user-defined identifiers:
and | break | do | else |
elseif | end | false | for |
function | if | in | local |
nil | not | or | repeat |
return | then | true | until |
while | goto |
It is generally agreed that names starting with an underscore followed by a series of uppercase letters (such as _VERSION) are reserved for Lua's internal global variables.
By default, variables are always considered global.
Global variables do not need to be declared; they are created when a variable is assigned a value. Accessing a global variable that has not been initialized will not cause an error, but the result will be: nil.
> print(b) nil > b=10 > print(b) 10 >
If you want to delete a global variable, you just need to assign nil to the variable.
b = nil print(b) --> nil
The variable b seems as if it has never been used. In other words, a variable exists only if it is not equal to nil.