Lua variable


Release date:2023-09-27 Update date:2023-10-13 Editor:admin View counts:233

Label:

Lua variable

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

Before the compiler executes the code, the compiler needs to know how to open up a storage area for statement variables to store the values of the variables.

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

All Lua variables are global variables, even in statement blocks or functions, unless you use the local explicitly declared as a local variable.

The scope of a local variable is from the declaration position to the end of the statement block.

The default values of variables are all 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 local variables
    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 the table field.

a = "hello" .. "world"
t.n = t.n + 1

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

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

Encountered assignment statement Lua all the values on the right are calculated first and then the assignment is performed, 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[j]'

When the number of variables is inconsistent with the number of values Lua , the following strategies will always be adopted based on the number of variables:

a. Number of variables>number of values            Completing nil by the number of variables
b. Number of variables<number of values            Excess values will be ignored

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 condition. 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-valued assignments are often used to exchange variables or to return function calls to variables:

a, b = f()

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

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

    1. Avoid naming conflicts.

    1. Accessing local variables is faster than global variables.

Indexes

Use square brackets for the index of table . Lua also provides . operation.

t[i]
t.i                 -- A simplified writing method when the index is of string type
gettable_event(t,i) -- Using index access is essentially a function call similar to this

Example

> site = {}
> site["key"] = "www.runoob.com"
> print(site["key"])
www.runoob.com
> print(site.key)
www.runoob.com

Powered by TorCMS (https://github.com/bukun/TorCMS).