Lua - 表



簡介

表是Lua中唯一可用的資料結構,它幫助我們建立不同型別的資料,例如陣列和字典。Lua 使用關聯陣列,可以用數字和字串(除了nil)作為索引。表沒有固定大小,可以根據需要增長。

Lua 在所有表示中都使用表,包括包的表示。當我們訪問方法 string.format 時,這意味著我們正在訪問字串包中可用的 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都指向同一塊記憶體。不會為b單獨分配記憶體。當a設定為nil時,表仍然可以透過b訪問。當沒有引用指向一個表時,Lua 的垃圾回收機制會負責清理過程,以便重新使用這些未引用的記憶體。

示例

下面是一個例子,用於解釋上述表的特性。

main.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("alternatetable 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
alternatetable 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])

根據可選的比較器引數對錶進行排序。

讓我們看一些上述函式的示例。

示例 - 表連線

我們可以使用 concat 函式連線兩個表,如下所示:

main.lua

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

示例 - 插入和刪除

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

main.lua

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

示例 - 排序表

我們經常需要按特定順序對錶進行排序。sort 函式按字母順序對錶中的元素進行排序。下面是一個示例。

main.lua

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
廣告
© . All rights reserved.