4.22. Lua table

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

Page Views: 9 views

table is a data structure of Lua to help us create different data types,such as arrays, dictionaries, etc.

Lua table uses associative arrays. You can index the array with any type of value, but this value cannot be nil .

Lua table is not fixed in size, so you can expand it according to your needs.

Lua also solves modules (module), packages (package), and objects (Object) through table. For example string.format indicates that table string isindexed using “format”.

4.22.1. Construction of table #

A constructor is an expression that creates and initializes a table. The watch is Lua something unique and powerful. The simplest constructor is``{}`` to create an empty table You can initialize the array directly:

-- initialization table mytable = {} -- specified value mytable[1]= "Lua" -- Remove Reference mytable = nil -- lua Garbage collection releases memory 

When we table an and set the element, and then assign a to b, both an and b point to the same memory. If an is set to nil then b can also access the element of table . If there is no specified variable pointing to the garbage collection mechanism of aPhine Lua, the corresponding memory will becleaned up.

The following example demonstrates the above description:

Example #

-- Simple table mytable = {} print("The type of mytable is ",type(mytable)) mytable[1]= "Lua" mytable["wow"] = "Before modification" print("The element with mytable index 1 is ", mytable[1]) print("The element with mytable index wow is ", mytable["wow"]) -- alternatetable and mytable refer to the same table alternatetable = mytable print("The element with an alternatetable index of 1 is ", alternatetable[1]) print("The element with mytable index wow is ", alternatetable["wow"]) alternatetable["wow"] = "After modification" print("The element with mytable index wow is ", mytable["wow"]) -- Release variables alternatetable = nil print("alternatetable is ", alternatetable) -- mytable can still be accessed print("The element with mytable index wow is ", mytable["wow"]) mytable = nil print("mytable is ", mytable) 

The result of the above code execution is:

The type of mytable is table The element with mytable index 1 is Lua The element with mytable index wow is Before modification The element with an alternatetable index of 1 is Lua The element with mytable index wow is Before modification The element with mytable index wow is Before modification Alternatetable is nil The element with mytable index wow is Before modification Mytable is nil 

4.22.2. Table operation #

The following is a list of common methods for Table operations:

Serial number

Method and use

1

Table.concat (table [, sep [, start [, end] ]): concat is the abbreviation of concatenate (linkage, connection). The table.concat () function lists allelements of the array portion of the specified table in the parameter from the start position to the end position, separated by the specified sep.

2

Table.insert (table [pos,] Value): insert an element with a value of value at the specified position (pos) in the array section of table. The pos parameter is optional and defaults to the end of the array section.

3

Table.maxn (table) specifies the largest key value of all positive key values in table. If there is no element with a positive key value, 0 is returned. (this method no longer exists after Lua5.2. This paper uses a custom function to implement it.)

4

Table.remove (table [, pos] Returns the element where part of the table array is in the pos position. Subsequent elements are moved forward. The posparameter is optional. The default is table length, which is deleted from the last element.

5

Table.sort (table [, comp] Sort the given table in ascending order.

Next, let’s take a look at examples of these methods.

《地理信息系统原理、技术与方法》  97

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