国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

Lua中的table學習筆記

2024-07-21 23:04:21
字體:
來源:轉載
供稿:網友

table 在 Lua 里是一種重要的數(shù)據(jù)結構,它可以說是其他數(shù)據(jù)結構的基礎,通常的數(shù)組、記錄、線性表、隊列、集合等數(shù)據(jù)結構都可以用 table 來表示,甚至連全局變量(_G)、模塊、元表(metatable)等這些重要的 Lua 元素都是 table 的結構。可以說,table  是一個強大而又神奇的東西。

table 特性

在之前介紹 Lua 數(shù)據(jù)類型時,也說過了 table 的一些特性,簡單列舉如下(詳情可查看之前的介紹):

1.table是一個“關聯(lián)數(shù)組”,數(shù)組的索引可以是數(shù)字或者是字符串
2.table 的默認初始索引一般以 1 開始
3.table 的變量只是一個地址引用,對 table 的操作不會產生數(shù)據(jù)影響
4.table 不會固定長度大小,有新數(shù)據(jù)插入時長度會自動增長
5.table 的方法函數(shù)

Lua 5.2.2 內置有以下 7 中對 table 操作的方法:

concat

函數(shù) table.concat 主要用來把表里的每個元素通過一個分隔符(separator)連接組合起來,用法:

 

復制代碼 代碼如下:

table.concat(table, sep,  start, end)

 

上面除 table 外, sep、start、end 這三個參數(shù)都是可選的,并且順序讀入的,如果沒指定傳入,函數(shù) concat 會采用默認值(分隔符 sep 的默認值是空字符, start 的默認值是 1, end 的默認值是數(shù)組部分的總長)去執(zhí)行。

復制代碼 代碼如下:

local tbl = {"apple", "pear", "orange", "grape"}
 
print(table.concat(tbl))
 
print(table.concat(tbl, "、"))
 
print(table.concat(tbl, "、", 2))
 
print(table.concat(tbl, "、", 2, 3))

 

對于密集型的字符并接,table.concat 比用 ".." 連接更高效,下面是用 time 測試的效果:

 

復制代碼 代碼如下:

local str = {}
for i = 1, 10000 do
    str[i] = "str"
end
print(table.concat(str))
--real  0m0.005s
--user  0m0.003s
--sys   0m0.002s

 

 

復制代碼 代碼如下:

local str = ""
for i = 1, 10000 do
    str = str .. "str"
end
print(str)
--real  0m0.041s
--user  0m0.037s
--sys   0m0.002s

 

insert

函數(shù) table.insert 用于向 table 的指定位置(pos)插入一個新元素,用法:

 

復制代碼 代碼如下:

table.insert(table, pos value)

 

參數(shù) pos 是可選,默認是 table 末尾位置。

 

復制代碼 代碼如下:

local tbl = {"apple", "pear", "orange", "grape"}
 
table.insert(tbl, "watermelon")
print(table.concat(tbl, "、"))
 
table.insert(tbl, 2, "watermelon")
print(table.concat(tbl, "、"))

 

maxn

函數(shù) table.maxn 是返回 table 最大的正數(shù)索引值,用法:

 

復制代碼 代碼如下:

table.maxn (table)

 

如果不存在正數(shù)的索引值,則返回 0。

 

復制代碼 代碼如下:

local tbl = {"apple", "pear", "orange", "grape"}
print(table.maxn(tbl))
 
local tbl = {"apple", "pear", "orange", "grape", [26] = "watermelon"}
print(table.maxn(tbl))
 
local tbl = {[-1] = "apple", [-2] = "pear", [-3] = "orange", [-4] = "grape"}
print(table.maxn(tbl))

 

pack

函數(shù) table.pack 是獲取一個索引從 1 開始的參數(shù)表 table,并會對這個 table 預定義一個字段 n,表示該表的長度,用法:

 

復制代碼 代碼如下:

table.pack(···)

 

該函數(shù)常用在獲取傳入函數(shù)的參數(shù)。

 

復制代碼 代碼如下:

function table_pack(param, ...)
    local arg = table.pack(...)
    print("this arg table length is", arg.n)
    for i = 1, arg.n do
        print(i, arg[i])
    end
end
 
table_pack("test", "param1", "param2", "param3")

 

remove

函數(shù) table.remove 用于刪除 table 里某個值,用法:

 

復制代碼 代碼如下:

table.remove (list [, pos])

參數(shù) pos 可選,默認為刪除 table 最后一個元素,并且參數(shù) pos 的類型只能是數(shù)字 number 類型。

 

 

復制代碼 代碼如下:

local tbl = {"apple", "pear", "orange", "grape"}
table.remove(tbl, 2)
print(table.concat(tbl, "、"))
 
table.remove(tbl)
print(table.concat(tbl, "、"))

 

sort

函數(shù) table.sort 用于對 table 里的元素作排序操作,用法:

 

復制代碼 代碼如下:

table.sort(table, comp)

 

參數(shù) comp 是一個排序對比函數(shù),它有兩個參數(shù) param1、param2,如果 param1 排在 param2 前面,那么排序函數(shù)返回 true,否則返回 false。

 

復制代碼 代碼如下:

local tbl = {"apple", "pear", "orange", "grape"}
local sort_func1 = function(a, b) return a > b end
table.sort(tbl, sort_func1)
print(table.concat(tbl, "、"))
 
local sort_func2 = function(a, b) return a < b end
table.sort(tbl, sort_func2)
print(table.concat(tbl, "、"))

 

參數(shù) comp 可選,缺省 comp 的情況下是對表作升序排序。

 

復制代碼 代碼如下:

local tbl = {"apple", "pear", "orange", "grape"}
table.sort(tbl)
print(table.concat(tbl, "、"))

 

unpack

函數(shù) table.unpack 用于返回 table 里的元素,用法:

 

復制代碼 代碼如下:

table.unpack(table, start, end)

 

參數(shù) start 是開始返回的元素位置,默認是 1,參數(shù) end 是返回最后一個元素的位置,默認是 table 最后一個元素的位置,參數(shù) start、end 都是可選

 

復制代碼 代碼如下:

local tbl = {"apple", "pear", "orange", "grape"}
print(table.unpack(tbl))
 
local a, b, c, d = table.unpack(tbl)
print(a, b, c, d)
 
print(table.unpack(tbl, 2))
print(table.unpack(tbl, 2, 3))
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 五指山市| 洪江市| 沈阳市| 沾化县| 台中市| 澎湖县| 全南县| 全椒县| 田林县| 普定县| 碌曲县| 宝鸡市| 吴忠市| 房山区| 视频| 都匀市| 莒南县| 双江| 平安县| 收藏| 抚顺县| 监利县| 克拉玛依市| 阿拉善盟| 治多县| 左权县| 垦利县| 灌南县| 南漳县| 松江区| 潞城市| 寻甸| 拉萨市| 准格尔旗| 民丰县| 隆昌县| 韶关市| 通山县| 巴彦县| 镇赉县| 临城县|