一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語(yǔ)言編程技術(shù)及教程分享平臺(tái)!
分類(lèi)導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Lua - Lua table類(lèi)型學(xué)習(xí)筆記

Lua table類(lèi)型學(xué)習(xí)筆記

2020-04-27 11:19腳本之家 Lua

這篇文章主要介紹了Lua table類(lèi)型學(xué)習(xí)筆記,本文講解了table的基礎(chǔ)知識(shí)和table庫(kù)函數(shù)的使用以及面向?qū)ο缶幊虒?shí)例,需要的朋友可以參考下

關(guān)系表類(lèi)型,這是一個(gè)很強(qiáng)大的類(lèi)型。我們可以把這個(gè)類(lèi)型看作是一個(gè)數(shù)組。只是 C語(yǔ)言的數(shù)組,只能用正整數(shù)來(lái)作索引; 在Lua中,你可以用任意類(lèi)型的值來(lái)作數(shù)組的索引,但這個(gè)值不能是 nil。同樣,在C語(yǔ)言中,數(shù)組的內(nèi)容只允許一種類(lèi)型;在 Lua中,你也可以用任意類(lèi)型的值來(lái)作數(shù)組的內(nèi)容,nil也可以。

基本介紹

注意三點(diǎn):
    第一,所有元素之間,總是用逗號(hào) "," 隔開(kāi);
    第二,所有索引值都需要用 "["和"]" 括起來(lái);如果是字符串,還可以去掉引號(hào)和中括號(hào); 即如果沒(méi)有[]括起,則認(rèn)為是字符串索引
    第三,如果不寫(xiě)索引,則索引就會(huì)被認(rèn)為是數(shù)字,并按順序自動(dòng)從 1往后編;

例如:

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


tt = {"hello" ,33}
value = 4
tab = {[tt] = "table",key = value, ["flag" ] = nil, 11}

 

print(tab[tt])
print(tab.key)
print(tab[1 ])


以上寫(xiě)法都是對(duì)的。

 

look = {[www] = "ok"}這樣是不對(duì)的,www沒(méi)有賦值,所以默認(rèn)為nil因此出錯(cuò)table index is nil

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

---
temp = 1
tab = {[temp] = 1, 11}


print(tab[temp]) --此時(shí)的結(jié)果是11,因?yàn)?1沒(méi)有顯式對(duì)應(yīng)的key,因此從1開(kāi)始,如果前面定義了,則覆蓋其value

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

---
temp = 2
tab = {[temp] = 1, 11}
temp = 1


print(tab[temp]) -- 結(jié)果是11,雖然定義時(shí)[temp] = 1,但是后來(lái)我們改變了temp的值,所以指向另外的key了

 


以上可知:

1.對(duì)于字符串,在{}定義時(shí),可以key = value, 也可以["flag"] = nil,索引都是string類(lèi)型,對(duì)于非nil類(lèi)型變量(包括字符串),都可以[variable]=value的方式
2.使用table時(shí),對(duì)于字符串,可以通過(guò).的方式訪問(wèn),也可以通過(guò)[]方式訪問(wèn)。tab[a],tab[b],只要a==b那么tab[a]可以訪問(wèn)到tab[b]的值
3.不管定義索引時(shí)用的是常量還是變量,最終table中value的索引key是常量,不會(huì)隨變量的改變而變化該value的key

嵌套

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

tb11= {tb12 = {bool = true}} -- simple, it's a table IN a table :)
-- Call magic!
print(tb11.tb12.bool ) -- works fine, since it's calling the key and value correctly.
print(tab11["tb12" ].bool ) --same as line 33
print(tab11.tb12 ["bool"]) --same as line 33
print(tab11["tb12" ]["bool"]) --same as line 33


修改table的value

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


--Altering a table's content. Basically manipulating the values of the keys.
lucky= {john="chips" ,jane ="lemonade",jolene="egg salad" }

 

lucky.jolene = "fruit salad" --changed the value to "fruit salad" instead of "egg salad"
lucky.jerry = "fagaso food" -- adding a new key-value pair to the container lucky.
lucky.john = nil -- remove john from giving anything or from being a key.

 

table的易變性

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


a = {}; b = a;
print(a == b)  --> true

 

c,d = {},{};

print(c == d) -->false

 

table庫(kù)函數(shù)使用
-----------------------------------------------------------
1. table.sort (table [, comp])
Sorts table elements in a given order, in-place, from table[1] to table[n], where n is the length of the table. If comp is given, then it must be a function that receives two table elements, and returns true when the first is less than the second (so that not comp(a[i+1],a[i]) will be true after the sort). If comp is not given, then the standard Lua operator < is used instead.
The sort algorithm is not stable; that is, elements considered equal by the given order may have their relative positions changed by the sort.

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


name = {"you" ,"me", "him","bill" }
--table.sort - only works with arrays!
table.sort(name)
for k, v in ipairs( name) do
     print( k,v)
end
--table.sort uses callbacks. a function that is writtent to be called by a library function.
function cmp( a, b)
     if string.sub(a,2 ,2) < string.sub(b,2 ,2) then
          return true
     else
          return false
     end
end

 

table.sort(name, cmp)
for k, v in ipairs( name) do
     print( k,v)
end

 

2. table.insert (table, [pos,] value)

Inserts element value at position pos in table, shifting up other elements to open space, if necessary. The default value for pos is n+1, where n is the length of the table so that a call table.insert(t,x) inserts x at the end of table t.

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

--table.insert --an easy to copy a table to another table or adding elements to an array.!
foo = {"a" ,"c", "d"}
bar = {}
function printt( table)
    for i=1 ,#table do
         print(i,table [i ])
    end
end
print("before insert:" )
printt(foo)
table.insert(foo,2 ,"b")
print("after insert" )
printt(foo)

 

3.  table.concat (table [, sep [, i [, j]]])

Given an array where all elements are strings or numbers, returns table[i]..sep..table[i+1] ··· sep..table[j]. The default value for sep is the empty string, the default for i is 1, and the default for j is the length of the table. If i is greater than j, returns the empty string.

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

--table.concat does what it implies. Takes an array and concates to one string.
num = {1 ,2, 3,4,5 ,6}
print(table.concat (num ,"<"))

 

4. table.remove (table [, pos])

Removes from table the element at position pos, shifting down other elements to close the space, if necessary. Returns the value of the removed element. The default value for pos is n, where n is the length of the table, so that a call table.remove(t) removes the last element of table t.

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

abc = {"a" ,"b", "c"}
print(table.remove (abc ,2))
print("abc length = " .. #abc)

 

5. table.maxn (table)

Returns the largest positive numerical index of the given table, or zero if the table has no positive numerical indices. (To do its job this function does a linear traversal of the whole table.)
--table.maxn

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


apple = {"a" ,"p",[ 5]="e"}
print(table.maxn (apple )) -- 5

 

duck = {[-2 ]=3,[- 1]=0}
print(table.maxn (duck )) -- 0

 

面向?qū)ο缶幊?/strong>

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


--note for a object to work, it needs a closure(inner function with an upvalue(a local value from a higher scope))
--note: the more closures made, the slower the program would run.
function mg1( n)
    local function get ()
         return n ;
    end
    local function inc (m )
        n = n +m ;
    end
    return {get = get, inc= inc}
end

 

object = mg1(50 )
print(object.get ())
print(object["get" ]())

object.inc(2 )
print(object.get ())

----------------------------------------
do
    local function get (o )
         return o.one
    end
    local function inc (self , two )
        self.one = self.one + two
    end
    function mg3 (one )
         return {one = one , get = get , inc = inc }
    end
end
a = mg3(50 )
a:get()
a.inc(a,2 )
print(a:get())

----------------------------------------
do
    local T = {};
    function T:get()
         return self.n ;
    end
    function T:inc(m)
        self.n = self.n + m ;
    end
    function mg4 ( n )
         return {n = n , get =T.get , inc =T.inc }
    end
end

c = mg4(30 )
print(c:get())
c:inc(4 )
print(c:get())

 

 

(完)

延伸 · 閱讀

精彩推薦
  • LuaLua簡(jiǎn)介、編譯安裝教程及變量等語(yǔ)法介紹

    Lua簡(jiǎn)介、編譯安裝教程及變量等語(yǔ)法介紹

    這篇文章主要介紹了Lua簡(jiǎn)介、編譯安裝教程及變量等語(yǔ)法介紹,本文同時(shí)講解了lua注釋語(yǔ)法、Lua命令行方式等內(nèi)容,需要的朋友可以參考下 ...

    junjie3632020-04-14
  • LuaLua中計(jì)算、執(zhí)行字符串中Lua代碼的方法

    Lua中計(jì)算、執(zhí)行字符串中Lua代碼的方法

    這篇文章主要介紹了Lua中計(jì)算、執(zhí)行字符串中Lua代碼的方法,類(lèi)似JavaScript中eval函數(shù)的功能,在Lua中也可以實(shí)現(xiàn),需要的朋友可以參考下 ...

    腳本之家6322020-04-30
  • LuaLua中table庫(kù)函數(shù)方法介紹

    Lua中table庫(kù)函數(shù)方法介紹

    這篇文章主要介紹了Lua中table庫(kù)函數(shù)方法介紹,本文講解了concat、insert、maxn、remove、sort、foreachi等方法,需要的朋友可以參考下 ...

    腳本之家2502020-04-17
  • LuaLua中的元方法__newindex詳解

    Lua中的元方法__newindex詳解

    這篇文章主要介紹了Lua中的元方法__newindex詳解,本文講解了查詢(xún)與更新、監(jiān)控賦值、通過(guò)table給另一個(gè)table賦值等內(nèi)容,需要的朋友可以參考下 ...

    笨木頭8872020-04-09
  • LuaLua實(shí)現(xiàn)__add方法重載示例

    Lua實(shí)現(xiàn)__add方法重載示例

    這篇文章主要介紹了Lua實(shí)現(xiàn)__add方法重載示例,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下 ...

    腳本之家7452020-04-24
  • LuaLua和C語(yǔ)言的交互詳解

    Lua和C語(yǔ)言的交互詳解

    這篇文章主要介紹了Lua和C語(yǔ)言的交互詳解,Lua和C語(yǔ)言通過(guò)棧完成交互,本文結(jié)合代碼實(shí)例詳細(xì)講解了交互的方法,需要的朋友可以參考下 ...

    果凍想3702020-04-14
  • LuaLua教程(二):基礎(chǔ)知識(shí)、類(lèi)型與值介紹

    Lua教程(二):基礎(chǔ)知識(shí)、類(lèi)型與值介紹

    這篇文章主要介紹了Lua教程(二):基礎(chǔ)知識(shí)、類(lèi)型與值介紹,本文講解了Hello World程序、代碼規(guī)范、全局變量、類(lèi)型與值等內(nèi)容,需要的朋友可以參考下 ...

    腳本之家5922020-04-28
  • Lua深入探究Lua中的解析表達(dá)式

    深入探究Lua中的解析表達(dá)式

    這篇文章主要介紹了深入探究Lua中的解析表達(dá)式,對(duì)于其語(yǔ)法部分的說(shuō)明和示例都超詳細(xì),極力推薦此文!需要的朋友可以參考下 ...

    腳本之家3542020-05-05
主站蜘蛛池模板: 無码一区中文字幕少妇熟女网站 | 免费在线公开视频 | www国产精品 | 精品国产福利在线 | 王小军怎么了最新消息 | 亚洲AV精品一区二区三区不卡 | 国产精品精品 | 校花在公车上被内射好舒 | 大杳蕉在线影院在线播放 | 成人网欧美亚洲影视图片 | 四虎影视4hutv最新地址在线 | 国产成人99精品免费观看 | 亚洲视频1 | 国产二区三区 | 99久久精品无码一区二区毛片 | 俄罗斯毛片免费大全 | sao虎在线精品永久在线 | 羞羞视频麻豆 | 色姑娘导航 | 美女张开腿让男人桶的 视频 | 精品AV综合导航 | 日本一卡二卡3卡四卡网站精品 | 91系列在线观看免费 | 高级黄色片| 娇妻与公陈峰姚瑶小说在线阅读 | 国产99视频精品免费视频免里 | 亚洲精品久久啪啪网站成年 | 久久热这里面只有精品 | 亚洲黄色图| 亚洲成人伦理 | 亚洲春黄在线观看 | 久9青青cao精品视频在线 | 91精品国产99久久 | aaaaa特级毛片 | videodesexo中国妞 | 国产精品视频色拍拍 | 男人jj视频| 国产码一区二区三区 | 校花的第一次好紧好爽 | 日本午夜vr影院新入口 | 精品在线免费观看视频 |