欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Lua中的table學(xué)習(xí)筆記

 更新時(shí)間:2014年12月14日 11:46:26   投稿:junjie  
這篇文章主要介紹了Lua中的table學(xué)習(xí)筆記,本文講解了table.concat、table.insert、table.maxn、table.pack、 table.remove、table.sort等方法的使用,需要的朋友可以參考下

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

table 特性

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

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

Lua 5.2.2 內(nèi)置有以下 7 中對(duì) table 操作的方法:

concat

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

復(fù)制代碼 代碼如下:

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

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

復(fù)制代碼 代碼如下:

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))

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

復(fù)制代碼 代碼如下:

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

復(fù)制代碼 代碼如下:

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)插入一個(gè)新元素,用法:

復(fù)制代碼 代碼如下:

table.insert(table, pos value)

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

復(fù)制代碼 代碼如下:

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ù)索引值,用法:

復(fù)制代碼 代碼如下:

table.maxn (table)

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

復(fù)制代碼 代碼如下:

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 是獲取一個(gè)索引從 1 開始的參數(shù)表 table,并會(huì)對(duì)這個(gè) table 預(yù)定義一個(gè)字段 n,表示該表的長(zhǎng)度,用法:

復(fù)制代碼 代碼如下:

table.pack(···)

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

復(fù)制代碼 代碼如下:

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 里某個(gè)值,用法:

復(fù)制代碼 代碼如下:

table.remove (list [, pos])

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

復(fù)制代碼 代碼如下:

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 用于對(duì) table 里的元素作排序操作,用法:

復(fù)制代碼 代碼如下:

table.sort(table, comp)

參數(shù) comp 是一個(gè)排序?qū)Ρ群瘮?shù),它有兩個(gè)參數(shù) param1、param2,如果 param1 排在 param2 前面,那么排序函數(shù)返回 true,否則返回 false。

復(fù)制代碼 代碼如下:

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 的情況下是對(duì)表作升序排序。

復(fù)制代碼 代碼如下:

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

unpack

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

復(fù)制代碼 代碼如下:

table.unpack(table, start, end)

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

復(fù)制代碼 代碼如下:

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))

相關(guān)文章

  • Lua中讓回調(diào)函數(shù)支持回調(diào)對(duì)象方法的解決方法

    Lua中讓回調(diào)函數(shù)支持回調(diào)對(duì)象方法的解決方法

    這篇文章主要介紹了Lua中讓回調(diào)支持對(duì)象方法,一般情況下,Lua中只支持回調(diào)一個(gè)函數(shù),本文方法實(shí)現(xiàn)可以回調(diào)一個(gè)對(duì)象的方法,需要的朋友可以參考下
    2014-12-12
  • Lua中break語句的使用方法詳解

    Lua中break語句的使用方法詳解

    這篇文章主要介紹了Lua中break語句的使用方法詳解,是Lua入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-05-05
  • Lua判斷Table是否為空的方法(空的table即{})

    Lua判斷Table是否為空的方法(空的table即{})

    這篇文章主要介紹了Lua判斷Table是否為空的方法(空的table即{}),如何判斷l(xiāng)ua中的table是否是空的table呢,本文就試驗(yàn)了多個(gè)方法,最后得出比較好的判斷方法,需要的朋友可以參考下
    2015-04-04
  • lua閉包的理解以及表與函數(shù)的幾種表達(dá)方法

    lua閉包的理解以及表與函數(shù)的幾種表達(dá)方法

    本文首先通過具體的例子講解了Lua中閉包的概念,然后總結(jié)了閉包的應(yīng)用場(chǎng)合,最后探討了Lua中閉包的實(shí)現(xiàn)原理,以及l(fā)ua中表與函數(shù)的3種表達(dá)方式的匯總
    2015-08-08
  • linux系統(tǒng)安裝Nginx Lua環(huán)境

    linux系統(tǒng)安裝Nginx Lua環(huán)境

    因項(xiàng)目需求,需要在Linux系統(tǒng)下搭建一套nginx+lua的開發(fā)環(huán)境,經(jīng)過一番摸索,現(xiàn)總結(jié)如下,希望大家能夠喜歡。
    2016-12-12
  • Lua和C++的通信流程代碼實(shí)例

    Lua和C++的通信流程代碼實(shí)例

    這篇文章主要介紹了Lua和C++的通信流程代碼實(shí)例,本文是上一篇的DEMO,本文用代碼講解Lua和C++之間的通信,需要的朋友可以參考下
    2014-09-09
  • Lua編程示例(七):協(xié)同程序基礎(chǔ)邏輯

    Lua編程示例(七):協(xié)同程序基礎(chǔ)邏輯

    這篇文章主要介紹了Lua編程示例(七):協(xié)同程序基礎(chǔ)邏輯,本文直接給出代碼實(shí)例,需要的朋友可以參考下
    2015-07-07
  • 詳解Lua中的if語句的使用方法

    詳解Lua中的if語句的使用方法

    這篇文章主要介紹了詳解Lua中的if語句的使用方法,是Lua入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-05-05
  • 最新評(píng)論