4.5. Lua variable

发布时间 : 2025-10-25 13:34:12 UTC      

Page Views: 9 views

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 .

4.5.1. 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 

4.5.2. 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 

4.5.3. Example #

> site = {} > site["key"] = "www.runoob.com" > print(site["key"]) www.runoob.com > print(site.key) www.runoob.com 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。