copy:http://blog.csdn.net/q277055799/article/details/8438926
Lua中的table函數庫
一部分的table函數只對其數組部分產生影響, 而另一部分則對整個table均產生影響. 下面會分開說明.
table.concat(table, sep, start, end)
concat是concatenate(連鎖, 連接)的縮寫. table.concat()函數列出參數中指定table的數組部分從start位置到end位置的所有元素, 元素間以指定的分隔符(sep)隔開。除了table外, 其他的參數都不是必須的, 分隔符的默認值是空字符, start的默認值是1, end的默認值是數組部分的總長.
sep, start, end這三個參數是順序讀入的, 所以雖然它們都不是必須參數, 但如果要指定靠後的參數, 必須同時指定前面的參數.
- tbl = {"alpha", "beta", "gamma"}
- print(table.concat(tbl, ":"))
- print(table.concat(tbl, nil, 1, 2))
- print(table.concat(tbl, "\n", 2, 3))
tbl = {"alpha", "beta", "gamma"}
print(table.concat(tbl, ":"))
print(table.concat(tbl, nil, 1, 2))
print(table.concat(tbl, "\n", 2, 3))
alpha:beta:gamma
alphabeta
beta
gamma
table.insert(table, pos, value)
table.insert()函數在table的數組部分指定位置(pos)插入值爲value的一個元素. pos參數可選, 默認爲數組部分末尾.
- tbl = {"alpha", "beta", "gamma"}
- table.insert(tbl, "delta")
- table.insert(tbl, "epsilon")
- table.insert(tbl, 3, "zeta")
- print(table.concat(tbl,","))
tbl = {"alpha", "beta", "gamma"}
table.insert(tbl, "delta")
table.insert(tbl, "epsilon")
table.insert(tbl, 3, "zeta")
print(table.concat(tbl,","))
alpha,beta,zeta,gamma,delta,epsilon
table.maxn(table)
table.maxn()函數返回指定table中所有正數key值中最大的key值. 如果不存在key值爲正數的元素, 則返回0. 此函數不限於table的數組部分.
- tbl = {[1] = "a", [2] = "b", [3] = "c", [26] = "z"}
- print(#tbl)
- print(table.maxn(tbl))
- tbl[91.32] = true
- print(table.maxn(tbl))
tbl = {[1] = "a", [2] = "b", [3] = "c", [26] = "z"}
print(#tbl)
print(table.maxn(tbl))
tbl[91.32] = true
print(table.maxn(tbl))
3 --因爲數組不連續
26
91.32
table.remove(table, pos)
table.remove()函數刪除並返回table數組部分位於pos位置的元素. 其後的元素會被前移. pos參數可選, 默認爲table長度, 即從最後一個元素刪起.
- tbl = {[1] = "a", [2] = "b", [3] = "c", [26] = "z"}
- table.remove(tbl)
- print(table.concat(tbl,","))
- table.remove(tbl,3)
- print(table.concat(tbl,","))
tbl = {[1] = "a", [2] = "b", [3] = "c", [26] = "z"}
table.remove(tbl)
print(table.concat(tbl,","))
table.remove(tbl,3)
print(table.concat(tbl,","))
a,b
a,b
table.sort(table, comp)
table.sort()函數對給定的table進行升序排序.
comp是一個可選的參數, 此參數是一個外部函數, 可以用來自定義sort函數的排序標準.
此函數應滿足以下條件: 接受兩個參數(依次爲a, b), 並返回一個布爾型的值, 當a應該排在b前面時, 返回true, 反之返回false.
- function sortAsc(a, b)
- return a > b
- end
- tbl = {"alpha", "beta", "gamma", "delta"}
- tblNum = {1,3,5,7,9,2,4,6,8,10}
- table.sort(tbl)
- table.sort(tblNum)
- print(table.concat(tbl, ", "))
- print(table.concat(tblNum, ", "))
- table.sort(tblNum, sortAsc)
- print(table.concat(tblNum, ", "))
function sortAsc(a, b)
return a > b
end
tbl = {"alpha", "beta", "gamma", "delta"}
tblNum = {1,3,5,7,9,2,4,6,8,10}
table.sort(tbl)
table.sort(tblNum)
print(table.concat(tbl, ", "))
print(table.concat(tblNum, ", "))
table.sort(tblNum, sortAsc)
print(table.concat(tblNum, ", "))
alpha, beta, delta, gamma
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
10, 9, 8, 7, 6, 5, 4, 3, 2, 1
用類似的原理還可以寫出更加複雜的排序函數. 例如, 有一個table存有工會三名成員的姓名及等級信息:
guild = {}
table.insert(guild, {
name = "Cladhaire",
class = "Rogue",
level = 70,
})
table.insert(guild, {
name = "Sagart",
class = "Priest",
level = 70,
})
table.insert(guild, {
name = "Mallaithe",
class = "Warlock",
level = 40,
})
對這個table進行排序時, 應用以下的規則: 按等級升序排序, 在等級相同時, 按姓名升序排序.
可以寫出這樣的排序函數:
function sortLevelNameAsc(a, b)
if a.level == b.level then
return a.name < b.name
else
return a.level < b.level
end
end
測試功能如下:
> table.sort(guild, sortLevelNameAsc)
> for idx, value in ipairs(guild) do print(idx, value.name) end
1, Mallaithe
2, Cladhaire
3, Sagart
table.foreachi(table, function(i, v))
會期望一個從 1(數字 1)開始的連續整數範圍,遍歷table中的key和value逐對進行function(i, v)操作
- t1 = {2, 4, 6, language="Lua", version="5", 8, 10, 12, web="hello lua"};
- table.foreachi(t1, function(i, v) print (i, v) end) ; --等價於foreachi(t1, print)
t1 = {2, 4, 6, language="Lua", version="5", 8, 10, 12, web="hello lua"};
table.foreachi(t1, function(i, v) print (i, v) end) ; --等價於foreachi(t1, print)
輸出結果:
1 2
2 4
3 6
4 8
5 10
6 12
table.foreach(table, function(i, v))
與foreachi不同的是,foreach會對整個表進行迭代
- t1 = {2, 4, 6, language="Lua", version="5", 8, 10, 12, web="hello lua"};
- table.foreach(t1, function(i, v) print (i, v) end) ;
t1 = {2, 4, 6, language="Lua", version="5", 8, 10, 12, web="hello lua"};
table.foreach(t1, function(i, v) print (i, v) end) ;
輸出結果:
1 2
2 4
3 6
4 8
5 10
6 12
web hello lua
language Lua
version 5
table.getn(table)
返回table中元素的個數
t1 = {1, 2, 3, 5};
print(getn(t1))
->4
table.setn(table, nSize)
設置table中的元素個數