English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Before a variable is used, it needs to be declared in the code, that is, to create the variable.
Before compiling and executing the code, the compiler needs to know how to allocate storage space for statement variables to store variable values.
Lua variables have three types: global variables, local variables, and table fields.
In Lua, all variables are global variables, even if they are in a statement block or a function, unless they are explicitly declared as local variables using local.
The scope of local variables in Lua is from the declaration position to the end of the statement block in which they are declared.
The default value of variables is nil.
-- test.lua script file a = 5 -- Global variables local b = 5 -- Local variables function joke() c = 5 -- Global variables local d = 6 -- Local variables end joke() print(c, d) --> 5 nil do local a = 6 -- Local variables b = 6 -- Reassigning local variables print(a, b); --> 6 6 end print(a, b) --> 5 6
The output result of the above example is executed as follows:
$ lua test.lua 5 nil 6 6 5 6
Assignment is the most basic method to change the value of a variable and modify a table field.
a = "hello" .. "world" t.n = t.n + 1
Lua can assign values to multiple variables at the same time, with each element of the variable list and value list separated by commas. The values on the right side of the assignment statement are assigned to the left side variables sequentially.
a, b = 10, 2*x <--> a =10; b =2*x
When encountering an assignment statement, Lua first calculates all the values on the right and then performs the assignment operation, so we can swap the values of variables in this way:
x, y = y, x -- swap 'x' for 'y' a[i], a[j] = a[j], a[i] -- swap 'a[i]' for 'a[j]'
When the number of variables and the number of values are not consistent, Lua will always use the following strategy based on the number of variables:
a. The number of variables > the number of values Additional nils will be added to match the number of variables b. The number of variables < the number of values Extra values will be ignored
a, b, c = 0, 1 print(a, b, c) --> 0 1 nil a, b = a+1, b+1, b+2 -- value of b+2 is ignored print(a, b) --> 1 2 a, b, c = 0 print(a, b, c) --> 0 nil nil
The last example above is a common mistake, note: when assigning to multiple variables, each variable must be assigned in order.
a, b, c = 0, 0, 0 print(a, b, c) --> 0 0 0
Multi-value assignment is often used to swap variables or assign function calls to variables:
a, b = f()
f() returns two values, the first assigned to a, and the second to b.
It is better to use local variables as much as possible, which has two advantages:
1. Avoid naming conflicts.
2. Accessing local variables is faster than global variables.
Indexing a table with square brackets []. Lua also provides the . operator.
t[i] t.i -- A simplified notation when the index is of string type gettable_event(t,i) -- Accessing by index is essentially a function call like this
> site = {} > site["key"] = "www.oldtoolbag.com" > print(site["key"]) www.oldtoolbag.com > print(site.key) www.oldtoolbag.com