Latest web development tutorials

Lua table (table)

Lua table is a data structure used to help us to create different types of data, such as: digital, dictionaries.

Lua table with the associated array, you can use any type of value to make an index of the array, but this value is not nil.

Lua table size is not fixed, you can expand according to their needs.

Lua also through the table to resolve the module (module), the package (package) and the object (Object) of. For example string.format indication "format" to index the table string.


table (Table) configuration

The constructor is an expression to create and initialize the table. Lua table is unique powerful stuff. The simplest constructor is {} used to create an empty table. You can directly initialize the array:

-- 初始化表
mytable = {}

-- 指定值
mytable[1]= "Lua"

-- 移除引用
mytable = nil
-- lua 垃圾回收会释放内存

When we table a set of elements, and then assign a to b, then a and b point to the same memory. If a set to nil, then b can also access the table of elements. If you do not specify the variable points to a, Lua's garbage collection will clean the corresponding memory.

The following example demonstrates the above description of the situation:

-- 简单的 table
mytable = {}
print("mytable 的类型是 ",type(mytable))

mytable[1]= "Lua"
mytable["wow"] = "修改前"
print("mytable 索引为 1 的元素是 ", mytable[1])
print("mytable 索引为 wow 的元素是 ", mytable["wow"])

-- alternatetable和mytable的是指同一个 table
alternatetable = mytable

print("alternatetable 索引为 1 的元素是 ", alternatetable[1])
print("mytable 索引为 wow 的元素是 ", alternatetable["wow"])

alternatetable["wow"] = "修改后"

print("mytable 索引为 wow 的元素是 ", mytable["wow"])

-- 释放变量
alternatetable = nil
print("alternatetable 是 ", alternatetable)

-- mytable 仍然可以访问
print("mytable 索引为 wow 的元素是 ", mytable["wow"])

mytable = nil
print("mytable 是 ", mytable)

The above code is executed as a result of:

mytable 的类型是 	table
mytable 索引为 1 的元素是 	Lua
mytable 索引为 wow 的元素是 	修改前
alternatetable 索引为 1 的元素是 	Lua
mytable 索引为 wow 的元素是 	修改前
mytable 索引为 wow 的元素是 	修改后
alternatetable 是 	nil
mytable 索引为 wow 的元素是 	修改后
mytable 是 	nil

Operating Table

The following Table lists the common methods of operation:

No. Method & Purpose
1 table.concat (table [, sep [, start [, end]]]):

concat is concatenate (chain link) abbreviation. table.concat () function specifies the parameters listed in the table array part of all elements from start position to the end position, spaced between elements in the specified separator (sep).

2 table.insert (table, [pos,] value):

Array part table specified location (pos) insert is an element of value. Pos parameter is optional and defaults to the end portion of the array.

3 table.maxn (table)

Specify the table key value for all positive numbers the largest key value. If the value is positive, the key element is not present, it returns 0. (After Lua5.2 this method does not exist, this article uses a custom function to achieve)

4 table.remove (table [, pos])

Returns an array of table portion is located at position pos element Thereafter element will be moved forward. Pos parameter is optional and defaults to the table length, starting from the last element deleted.

5 table.sort (table [, comp])

For a given table in ascending order.

Next we look at the examples of these methods.

Table connection

We can use concat () method to connect two table:

fruits = {"banana","orange","apple"}
-- 返回 table 连接后的字符串
print("连接后的字符串 ",table.concat(fruits))

-- 指定连接字符
print("连接后的字符串 ",table.concat(fruits,", "))

-- 指定索引来连接 table
print("连接后的字符串 ",table.concat(fruits,", ", 2,3))

Execute the above code output results:

连接后的字符串 	bananaorangeapple
连接后的字符串 	banana, orange, apple
连接后的字符串 	orange, apple

Insertion and removal

The following example demonstrates the table insertion and removal operations:

fruits = {"banana","orange","apple"}

-- 在末尾插入
table.insert(fruits,"mango")
print("索引为 4 的元素为 ",fruits[4])

-- 在索引为 2 的键处插入
table.insert(fruits,2,"grapes")
print("索引为 2 的元素为 ",fruits[2])

print("最后一个元素为 ",fruits[5])
table.remove(fruits)
print("移除后最后一个元素为 ",fruits[5])

Execute the above code output results:

索引为 4 的元素为 	mango
索引为 2 的元素为 	grapes
最后一个元素为 	mango
移除后最后一个元素为 	nil

Table sorting

The following example demonstrates the sort () method of use, for Table Sort:

fruits = {"banana","orange","apple","grapes"}
print("排序前")
for k,v in ipairs(fruits) do
	print(k,v)
end

table.sort(fruits)
print("排序后")
for k,v in ipairs(fruits) do
	print(k,v)
end

Execute the above code output results:

排序前
1	banana
2	orange
3	apple
4	grapes
排序后
1	apple
2	banana
3	grapes
4	orange

Table Max

table.maxn after Lua5.2 this method does not exist, we define table_maxn methods.

The following example demonstrates how to obtain the maximum value table:

function table_maxn(t)
    local mn = 0
    for k, v in pairs(t) do
        if mn < k then
            mn = k
        end
    end
    return mn
end
tbl = {[1] = "a", [2] = "b", [3] = "c", [26] = "z"}
print("tbl 长度 ", #tbl)
print("tbl 最大值 ", table_maxn(tbl))

Execute the above code output results:

tbl 长度 	3
tbl 最大值 	26