Lua Variables

Before a variable is used, it needs to be declared in the code, that is, the variable must be created.

Before executing code, the compiler needs to know how to allocate storage for statement variables to store their values.

Lua variables have three types: global variables, local variables, and fields in tables.

All variables in Lua are global variables, even in statement blocks or functions, unless explicitly declared as local variables using `local`.

The scope of a local variable is from the position of its declaration to the end of the statement block in which it is located.

The default value of variables is nil.

Example

-- test.lua file script
a = 5               -- Global variable
local b = 5         -- Local variable

function joke()
    c = 5           -- Global variable
    local d = 6     -- Local variable
end

joke()
print(c,d)          --> 5 nil

do
    local a = 6     -- Local variable
    b = 6           -- Reassign the local variable
    print(a,b);     --> 6 6
end

print(a,b)      --> 5 6

Executing the above example produces the following output:

$ lua test.lua 
5    nil
6    6
5    6

Assignment statement

Assignment is the most basic way to change the value of a variable and to change a table field.

a = "hello" .. "world"
t.n = t.n + 1
Lua can assign values to multiple variables at the same time. The elements of the variable list and the value list are separated by commas. The values on the right side of the assignment statement are assigned to the variables on the left in order.
a, b = 10, 2*x       <-->       a=10; b=2*x

When encountering an assignment statement, Lua first evaluates all the values on the right and then performs the assignment operation, so we can swap variable values 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 inconsistent, Lua always takes the number of variables as the basis and adopts the following strategy:

a. 变量个数 > 值的个数             按变量个数补足nil
b. 变量个数 < 值的个数             多余的值会被忽略

Example

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 error situation. Note: if you want to assign values to multiple variables, you must assign each variable in turn.

a, b, c = 0, 0, 0
print(a,b,c)             --> 0   0   0

Multiple assignment is often used to swap variables or to assign the return values of a function call to variables:

a, b = f()

f() returns two values, the first is assigned to a, and the second is assigned to b.

You should use local variables as much as possible. There are two benefits:

  • 1. Avoid naming conflicts.
  • 2. Accessing local variables is faster than global variables.

Indexing

Use square brackets [] to index tables. Lua also provides the . operation.

t[i]
t.i                 -- 当索引为字符串类型时的一种简化写法
gettable_event(t,i) -- 采用索引访问本质上是一个类似这样的函数调用

Example

> site = {}
> site["key"] = "www.example.com"
> print(site["key"])
www.example.com
> print(site.key)
www.example.com
Other extensions