Latest web development tutorials

# Lua variables

Variables before use, must be declared in the code that created the variable.

Before performing the compiler code compiler needs to know how to open up the statement variable memory area for storing the value of the variable.

Lua variables There are three types: global variables, local variables, table fields.

Lua variables in all global variables, or even a block of statements in function, with local unless explicitly declared as a local variable.

The scope of local variables is to start from a position where the end of the statement block.

The default values ​​of the 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
```

Perform the above example output is:

```\$ lua test.lua
5	nil
6	6
5	6
```

## Assignment

Assignment is to change the value of a variable and changing the basic method table field.

```a = "hello" .. "world"
t.n = t.n + 1
```
Lua can assign multiple variables at the same time, a list of variables and a list of values ​​for each element separated by a comma, the value of the right side of the assignment statement in turn assigned to the variable on the left.
```a, b = 10, 2*x       <-->       a=10; b=2*x
```

Lua encountered assignment will first calculate all values ​​to the right and then perform the assignment, so we can exchange the value of this variable were:

```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 the number of variables and values ​​are inconsistent, Lua will be the number of variables has been taken on the basis of the following strategies:

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

E.g:

```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 conditions Note: If you want to assign multiple variables must be sequentially assigned to each variable.

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

More often used to exchange variable value assignment or a function call to return to the variable:

```a, b = f()
```

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

Should be possible to use local variables, there are two benefits:

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

## index

The index of the table using square brackets []. Lua also provides operating.

```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
```