Lua表


表是Lua中唯一可用的資料結構,使用表可以建立不同的型別,如陣列和字典。 Lua使用關聯陣列,不僅可使用數位編製索引,還可以使用除nil之外的字串編製索引。 表沒有固定的大小,可以根據需要增長大小。

Lua在所有表示中使用表,包括包的表示。 當存取方法string.format時,這意味著,存取字串包中可用的格式函式。

表示和用法

表稱為物件,它既不是值也不是變數。 Lua使用建構函式表示式{}來建立一個空表。 應該知道,保持表的參照的變數和表本身之間沒有固定的關係。

--sample table initialization
mytable = {}

--simple table value assignment
mytable[1]= "Lua"

--removing reference
mytable = nil

-- lua garbage collection will take care of releasing memory

假設有一個包含元素集的表a,如果將它分配給表b,則ab都指向相同的記憶體。 Lua不會單獨為b分配單獨的記憶體。 當a設定為nil時,b仍然可以存取表。 當沒有對表的參照時,Lua中的垃圾收集器負責清理進程以使這些未參照的記憶體再次被重用。

下面示出了一個例子,用於解釋表的上述特徵。

-- Simple empty table
mytable = {}
print("Type of mytable is ",type(mytable))

mytable[1]= "Lua"
mytable["wow"] = "Tutorial"

print("mytable Element at index 1 is ", mytable[1])
print("mytable Element at index wow is ", mytable["wow"])

-- alternatetable and mytable refers to same table
alternatetable = mytable

print("alternatetable Element at index 1 is ", alternatetable[1])
print("mytable Element at index wow is ", alternatetable["wow"])

alternatetable["wow"] = "I changed it"

print("mytable Element at index wow is ", mytable["wow"])

-- only variable released and and not table
alternatetable = nil
print("alternatetable is ", alternatetable)

-- mytable is still accessible
print("mytable Element at index wow is ", mytable["wow"])

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

當執行上述程式時,將獲得以下輸出 -

Type of mytable is     table
mytable Element at index 1 is     Lua
mytable Element at index wow is     Tutorial
alternatetable Element at index 1 is     Lua
mytable Element at index wow is     Tutorial
mytable Element at index wow is     I changed it
alternatetable is     nil
mytable Element at index wow is     I changed it
mytable is     nil

表操作

下面是用於表操作的內建函式,它們列在下表格中。

編號 方法 作用
1 table.concat (table [, sep [, i [, j]]]) 根據給定的引數連線表中的字串。詳細資訊請參見範例。
2 table.insert (table, [pos,] value) 在指定位置的表中插入值。
3 table.maxn (table) 返回最大的數位索引。
4 table.remove (table [, pos]) 從表中刪除值。
5 table.sort (table [, comp]) 根據可選的比較器引數對表進行排序。

下面來看看一些上述功能的範例。

1. 表連線

使用concat函式來連線兩個表,如下所示 -

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

-- returns concatenated string of table
print("Concatenated string ",table.concat(fruits))

--concatenate with a character
print("Concatenated string ",table.concat(fruits,", "))

--concatenate fruits based on index
print("Concatenated string ",table.concat(fruits,", ", 2,3))

當執行上述程式時,將獲得以下輸出 -

Concatenated string     bananaorangeapple
Concatenated string     banana, orange, apple
Concatenated string     orange, apple

2. 插入和刪除

在表操作中,最常見的是在表中插入和刪除專案。如下解釋。

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

-- insert a fruit at the end
table.insert(fruits,"mango")
print("Fruit at index 4 is ",fruits[4])

--insert fruit at index 2
table.insert(fruits,2,"grapes")
print("Fruit at index 2 is ",fruits[2])

print("The maximum elements in table is",table.maxn(fruits))

print("The last element is",fruits[5])

table.remove(fruits)
print("The previous last element is",fruits[5])

當執行上述程式時,將獲得以下輸出 -

Fruit at index 4 is     mango
Fruit at index 2 is     grapes
The maximum elements in table is    5
The last element is    mango
The previous last element is    nil

3. 排序表

有時想要按特定順序對表進行排序。 排序函式按字母順序對表中的元素進行排序。 這方面的範例如下所示。

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

for k,v in ipairs(fruits) do
   print(k,v)
end

table.sort(fruits)
print("sorted table")

for k,v in ipairs(fruits) do
   print(k,v)
end

當執行上述程式時,將獲得以下輸出 -

1    banana
2    orange
3    apple
4    grapes
sorted table
1    apple
2    banana
3    grapes
4    orange