自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺析Lua中關(guān)于Table函數(shù)庫

移動(dòng)開發(fā) iOS
Lua中關(guān)于Table函數(shù)庫是本文要介紹的內(nèi)容,主要是來學(xué)習(xí)Table函數(shù)庫在lua中如何使用,具體來看本文詳解。一部分的table函數(shù)只對其數(shù)組部分產(chǎn)生影響, 而另一部分則對整個(gè)table均產(chǎn)生影響.

Lua中關(guān)于Table函數(shù)庫是本文要介紹的內(nèi)容,主要是來學(xué)習(xí)Table函數(shù)庫在lua中如何使用,具體來看本文詳解。一部分的table函數(shù)只對其數(shù)組部分產(chǎn)生影響, 而另一部分則對整個(gè)table均產(chǎn)生影響. 下面會(huì)分開說明.

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

concat是concatenate(連鎖, 連接)的縮寫. table.concat()函數(shù)列出參數(shù)中指定table的數(shù)組部分從start位置到end位置的所有元素, 元素間以指定的分隔符(sep)隔開。除了table外, 其他的參數(shù)都不是必須的, 分隔符的默認(rèn)值是空字符, start的默認(rèn)值是1, end的默認(rèn)值是數(shù)組部分的總長.

sep, start, end這三個(gè)參數(shù)是順序讀入的, 所以雖然它們都不是必須參數(shù), 但如果要指定靠后的參數(shù), 必須同時(shí)指定前面的參數(shù).

  1. > tbl = {"alpha", "beta", "gamma"}  
  2. > print(table.concat(tbl, ":"))  
  3. alpha:beta:gamma  
  4. > print(table.concat(tbl, nil, 1, 2))  
  5. alphabeta  
  6. > print(table.concat(tbl, "\n", 2, 3))  
  7. beta  
  8. gamma  
  9. table.insert(table, pos, value)  

table.insert()函數(shù)在table的數(shù)組部分指定位置(pos)插入值為value的一個(gè)元素. pos參數(shù)可選, 默認(rèn)為數(shù)組部分末尾.

  1. > tbl = {"alpha", "beta", "gamma"}  
  2. > table.insert(tbl, "delta")  
  3. > table.insert(tbl, "epsilon")  
  4. > print(table.concat(tbl, ", ")  
  5. alpha, beta, gamma, delta, epsilon  
  6. > table.insert(tbl, 3, "zeta")  
  7. > print(table.concat(tbl, ", ")  
  8. alpha, beta, zeta, gamma, delta, epsilon  
  9. table.maxn(table)  

table.maxn()函數(shù)返回指定table中所有正數(shù)key值中***的key值. 如果不存在key值為正數(shù)的元素, 則返回0. 此函數(shù)不限于table的數(shù)組部分.

  1. > tbl = {[1] = "a", [2] = "b", [3] = "c", [26] = "z"}  
  2. > print(#tbl)  
  3. 3               -- 因?yàn)?6和之前的數(shù)字不連續(xù), 所以不算在數(shù)組部分內(nèi)  
  4. > print(table.maxn(tbl))  
  5. 26  
  6. > tbl[91.32] = true  
  7. > print(table.maxn(tbl))  
  8. 91.32  
  9. table.remove(table, pos)  

table.remove()函數(shù)刪除并返回table數(shù)組部分位于pos位置的元素. 其后的元素會(huì)被前移. pos參數(shù)可選, 默認(rèn)為table長度, 即從***一個(gè)元素刪起.

  1. table.sort(table, comp)  

table.sort()函數(shù)對給定的table進(jìn)行升序排序.

  1. > tbl = {"alpha", "beta", "gamma", "delta"}  
  2. > table.sort(tbl)  
  3. > print(table.concat(tbl, ", "))  
  4. alpha, beta, delta, gamma 

comp是一個(gè)可選的參數(shù), 此參數(shù)是一個(gè)外部函數(shù), 可以用來自定義sort函數(shù)的排序標(biāo)準(zhǔn).

此函數(shù)應(yīng)滿足以下條件: 接受兩個(gè)參數(shù)(依次為a, b), 并返回一個(gè)布爾型的值, 當(dāng)a應(yīng)該排在b前面時(shí), 返回true, 反之返回false.

例如, 當(dāng)我們需要降序排序時(shí), 可以這樣寫:

  1. > sortFunc = function(a, b) return b < a end  
  2. > table.sort(tbl, sortFunc)  
  3. > print(table.concat(tbl, ", "))  
  4. gamma, delta, beta, alpha 

用類似的原理還可以寫出更加復(fù)雜的排序函數(shù). 例如, 有一個(gè)table存有工會(huì)三名成員的姓名及等級(jí)信息:

  1. guild = {}  
  2.  
  3. table.insert(guild, {  
  4.  name = "Cladhaire",  
  5.  class = "Rogue",  
  6.  level = 70,  
  7. })  
  8.  
  9. table.insert(guild, {  
  10.  name = "Sagart",  
  11.  class = "Priest",  
  12.  level = 70,  
  13. })  
  14.  
  15. table.insert(guild, {  
  16.  name = "Mallaithe",  
  17.  class = "Warlock",  
  18.  level = 40,  
  19. }) 

對這個(gè)table進(jìn)行排序時(shí), 應(yīng)用以下的規(guī)則: 按等級(jí)升序排序, 在等級(jí)相同時(shí), 按姓名升序排序.

可以寫出這樣的排序函數(shù):

  1. function sortLevelNameAsc(a, b)  
  2.  if a.level == b.level then  
  3.   return a.name < b.name 
  4.  else  
  5.   return a.level < b.level 
  6.  end  
  7. end 

測試功能如下:

  1. table.sort(guild, sortLevelNameAsc)  
  2.  for idx, value in ipairs(guild) do print(idx, value.name) end  
  3. 1, Mallaithe  
  4. 2, Cladhaire  
  5. 3, Sagart  
  6. table.foreachi(table, function(i, v)) 

會(huì)期望一個(gè)從 1(數(shù)字 1)開始的連續(xù)整數(shù)范圍,遍歷table中的key和value逐對進(jìn)行function(i, v)操作

  1. t1 = {2, 4, 6, language="Lua"version="5", 8, 10, 12, web="hello lua"};  
  2. table.foreachi(t1, function(i, v) print (i, v) end) ; --等價(jià)于foreachi(t1, print) 

輸出結(jié)果:

  1. 1 2  
  2. 2 4  
  3. 3 6  
  4. 4 8  
  5. 5 10  
  6. 6 12  
  7. table.foreach(table, function(i, v)) 

與foreachi不同的是,foreach會(huì)對整個(gè)表進(jìn)行迭代

t1 = {2, 4, 6, language="Lua", version="5", 8, 10, 12, web="hello lua"};
table.foreach(t1, function(i, v) print (i, v) end) ;

輸出結(jié)果:

  1. 1 2  
  2. 2 4  
  3. 3 6  
  4. 4 8  
  5. 5 10  
  6. 6 12  
  7. web hello lua  
  8. language Lua  
  9. version 5  
  10. table.getn(table) 

返回table中元素的個(gè)數(shù)

  1. t1 = {1, 2, 3, 5};  
  2. print(getn(t1))  
  3. ->4  
  4. table.setn(table, nSize) 

設(shè)置table中的元素個(gè)數(shù)。

小結(jié):關(guān)于淺析Lua中關(guān)于Table函數(shù)庫的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對你有所幫助!

責(zé)任編輯:zhaolei 來源: 博客園
相關(guān)推薦

2011-08-23 14:26:07

Lua字符串

2011-08-23 16:14:27

Lua函數(shù)庫函數(shù)

2009-09-16 10:43:22

PHP正則表達(dá)式函數(shù)

2009-09-17 13:15:12

NIS函數(shù)庫

2010-04-27 09:55:15

2011-08-23 16:37:05

Lua數(shù)學(xué)庫

2011-08-23 10:29:13

LuaPlayer

2011-08-23 16:22:45

Lua 4.0函數(shù)

2009-12-08 19:34:26

PHP拼寫檢查函數(shù)庫

2011-08-22 17:13:00

LuaC++函數(shù)

2011-08-23 16:48:41

Lua 5.1API 函數(shù)

2020-11-16 12:40:55

Java開發(fā)機(jī)器學(xué)習(xí)

2010-06-13 10:18:08

MySQL 數(shù)據(jù)庫函數(shù)

2011-08-23 17:33:08

LuaMetatable

2014-01-07 14:53:37

Android開發(fā)依賴注入Roboguice

2009-11-16 15:07:23

PHP數(shù)組函數(shù)庫

2021-07-10 07:41:20

Python中文函數(shù)庫Python基礎(chǔ)

2016-02-17 16:04:42

2011-08-24 17:09:35

LUA閉包函數(shù)

2011-08-25 17:01:50

LUA網(wǎng)游游戲
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)