Lua learning (4)--Lua variables

Before a variable can be used, it must be declared in the code, that is, the variable is created.
Before the compiler executes the code, the compiler needs to know how to create a memory area for the statement variable to store the value of the variable.
There are three types of Lua variables: global variables, local variables, and fields in tables.
Variables in Lua are all global variables, even in statement blocks or functions, unless they are explicitly declared as local variables with local.
The scope of a local variable is from the point of declaration to the end of the block in which it is located.
The default value of variables is 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 result of executing the above example is:

$ 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 change table fields.

a = "hello" .. "world"

Lua can assign multiple variables at the same time . The elements of the variable list and the value list are separated by commas. The value on the right side of the assignment statement will be assigned to the variable on the left side in turn.

a, b = 10, 2       <-->       a=10; b=2

When encountering an assignment statement, Lua will first calculate all the values ​​on the right side and then perform the assignment operation, so we can exchange the values ​​of variables like this:

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 will always adopt the following strategies based on the number of variables :

a. 变量个数 > 值的个数             按变量个数补足nil
b. 变量个数 < 值的个数             多余的值会被忽略
a, b, c = 0, 1
print(a,b,c)             --> 0   1   nil

a, b = 1, 2, 3     
print(a,b)               --> 1   2

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

The last example above is a common error condition. Note: 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

Multivalue assignment is often used to swap variables, or to return a function call to a variable:
f() returns two values, the first to a and the second to b.

a, b = f()

Local variables should be used as much as possible , there are two advantages:
1. Avoid naming conflicts.
2. Accessing local variables is faster than global variables.

index

Use square brackets [] for indexes into the table. Lua also provides the . operator.

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

E.g:

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325588324&siteId=291194637