詳解Lua中的表的概念及其相關(guān)操作方法
表格是唯一的數(shù)據(jù)結(jié)構(gòu)中Lua可以幫助我們創(chuàng)造出不同的類型,如數(shù)組和字典。 Lua使用關(guān)聯(lián)數(shù)組和可不僅數(shù)字,但也有不同的零字符串索引。表格都沒有固定的大小,并根據(jù)需要可以增長(zhǎng)。
Lua采用的所有陳述,包括包裝的代表性表。當(dāng)我們?cè)L問(wèn)一個(gè)方法的字符串。格式,這意味著,我們正在訪問(wèn)的格式化功能的字符串封裝。
表示和用法
表稱為對(duì)象和它們既不值,也沒有變。 Lua使用構(gòu)造函數(shù)表達(dá)式{}創(chuàng)建一個(gè)空表。它是要知道,有保存表的參考和表本身的變量之間沒有固定的關(guān)系。
mytable = {}
--simple table value assignment
mytable[1]= "Lua"
--removing reference
mytable = nil
-- lua garbage collection will take care of releasing memory
當(dāng)我們有一個(gè)表與集合的元素,如果我們將其指定為b,a和b都指向相同的內(nèi)存。沒有單獨(dú)的內(nèi)存單獨(dú)分配對(duì)b。當(dāng)設(shè)置為無(wú),表將仍然可以訪問(wèn)到b。當(dāng)沒有引用表,然后在Lua垃圾收集需要清理過(guò)程,使這些未引用的內(nèi)存再次被重用。
一個(gè)例子如下所示用于說(shuō)明表的上述特征。
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)
當(dāng)我們運(yùn)行上面的程序,會(huì)得到下面的輸出
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
表操作
在對(duì)表操作內(nèi)置函數(shù)和它們被列于下表中。
讓我們看看上面的函數(shù)一些例子。
表串聯(lián)
我們可以使用concat函數(shù)來(lái)連接,如下所示的兩個(gè)表。
-- 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))
當(dāng)我們運(yùn)行上面的程序,會(huì)得到下面的輸出
Concatenated string banana, orange, apple
Concatenated string orange, apple
插入和刪除
插入在表中的項(xiàng)目,并除去最常見于表操縱。它下面的解釋。
-- 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])
當(dāng)我們運(yùn)行上面的程序,會(huì)得到下面的輸出
Fruit at index 2 is grapes
The maximum elements in table is 5
The last element is mango
The previous last element is nil
排序表格
排序表通常需要和排序函數(shù)表中的元素按字母順序排序。下圖所示為這方面的一個(gè)范例。
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
當(dāng)我們運(yùn)行上面的程序,會(huì)得到下面的輸出
2 orange
3 apple
4 grapes
sorted table
1 apple
2 banana
3 grapes
4 orange
相關(guān)文章
Ruby中使用each和collect進(jìn)行迭代的用法
這篇文章主要介紹了Ruby中使用each和collect進(jìn)行迭代的用法,是Ruby學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-05-05Ruby設(shè)計(jì)模式編程中使用Builder建造者模式的實(shí)例
這篇文章主要介紹了Ruby設(shè)計(jì)模式編程中使用Builder建造者模式的實(shí)例,建造者模式將一個(gè)復(fù)雜對(duì)象的構(gòu)造與它的表示分離,使同樣的構(gòu)建過(guò)程可以創(chuàng)建不同的表示,需要的朋友可以參考下2016-03-03簡(jiǎn)單談?wù)凴uby的private和protected
Ruby類下對(duì)訪問(wèn)控制的限制也是用public,protected和private來(lái)做的。雖然用的是和C++和Java相同的名字,但是Ruby下的protected和private定義還是有所不同的。2016-02-02Ruby、PHP、Shell實(shí)現(xiàn)求50以內(nèi)的素?cái)?shù)
這篇文章主要介紹了Ruby、PHP、Shell實(shí)現(xiàn)求50以內(nèi)的素?cái)?shù),3種語(yǔ)言的實(shí)現(xiàn)方法中Shell最簡(jiǎn)單,PHP最麻煩,Ruby最簡(jiǎn)潔,需要的朋友可以參考下2015-01-01Ruby使用REXML庫(kù)來(lái)解析xml格式數(shù)據(jù)的方法
這篇文章主要介紹了Ruby使用REXML庫(kù)來(lái)解析xml格式數(shù)據(jù)的方法,文章最后提及了REXML庫(kù)的使用相關(guān)安全問(wèn)題可以注意一下,需要的朋友可以參考下2016-04-04Ruby中XML格式數(shù)據(jù)處理庫(kù)REXML的使用方法指南
這篇文章主要介紹了Ruby中XML格式數(shù)據(jù)處理庫(kù)REXML的使用方法指南,值得注意的REXML庫(kù)處理XML字符串時(shí)的編碼問(wèn)題,是需要的朋友可以參考下2016-04-04詳細(xì)解讀Ruby當(dāng)中的條件判斷語(yǔ)句
這篇文章主要介紹了詳細(xì)解讀Ruby當(dāng)中的條件判斷語(yǔ)句,if、else等邏輯判斷語(yǔ)句是各門編程語(yǔ)言的基礎(chǔ),需要的朋友可以參考下2015-05-05