Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Lua variable


May 12, 2021 Lua


Table of contents


Lua variable

Before a variable can be used, it must be declared in code, i.e. it is created. The compiler needs to know how to open up a store for statement variables to store the values of variables before the compiler executes the code.

There are three types of Lua variables: global variables, local variables, and fields in tables.

Variables outside the function default to global variables unless declared with local display. T he parameters of variables and functions within a function default to local variables.

The scope of a local variable is from the declared position to the end of the statement block (or until the declaration of the next local variable with the same name).

The default values for variables are nil.

-- test.lua 文件脚本
a = 5               -- 全局变量
local b = 5         -- 局部变量

function joke()
    c = 5           -- 全局变量
    local d = 6     -- 局部变量
end

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

do 
    local a = 6     -- 局部变量
    b = 6           -- 全局变量
    print(a,b);     --> 6 6
end

print(a,b)      --> 5 6

The output of the above instances is:

$ lua test.lua 
5	nil
6	6
5	6

The assignment statement

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

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

When we encounter the assignment statement Lua calculates all the values on the right before performing the assignment operation, so we can exchange the values of the variables like this:

x, y = y, x                     -- swap 'x' for 'y'
a[i], a[j] = a[j], a[i]         -- swap 'a[i]' for 'a[i]'

When the number of variables and the number of values do not agree, Lua always adopts the following strategy based on the number of variables:

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

For 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 scenario, note that if you want to assign values to multiple variables, you must assign values to each variable in turn.

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

Multi-value assignments are often used to exchange variables or return function calls to variables:

a, b = f()

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

Local variables should be used as much as possible, with two benefits:

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

Index

Use square brackets for the index of table. L ua also offers . O peration.

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

For example:

> site = {}
> site["key"] = "www.w3cschool.cn"
> print(site["key"])
www.w3cschool.cn
> print(site.key)
www.w3cschool.cn