亚洲综合原千岁中文字幕_国产精品99久久久久久久vr_无码人妻aⅴ一区二区三区浪潮_成人h动漫精品一区二区三

主頁 > 知識庫 > Lua table類型學(xué)習(xí)筆記

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

熱門標(biāo)簽:清朝地圖標(biāo)注哈爾濱 冀州市地圖標(biāo)注 漳州智云呼電話機器人 新岸線智能電銷機器人 武漢外呼防封系統(tǒng)多少錢 地圖標(biāo)注大廈 百度地圖標(biāo)注早餐區(qū)域 怎么去除地圖標(biāo)注 個人怎么在地圖標(biāo)注需要的店鋪

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

基本介紹

注意三點:
    第一,所有元素之間,總是用逗號 "," 隔開;
    第二,所有索引值都需要用 "["和"]" 括起來;如果是字符串,還可以去掉引號和中括號; 即如果沒有[]括起,則認為是字符串索引
    第三,如果不寫索引,則索引就會被認為是數(shù)字,并按順序自動從 1往后編;

例如:

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

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

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


以上寫法都是對的。

look = {[www] = "ok"}這樣是不對的,www沒有賦值,所以默認為nil因此出錯table index is nil

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

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

print(tab[temp]) --此時的結(jié)果是11,因為11沒有顯式對應(yīng)的key,因此從1開始,如果前面定義了,則覆蓋其value
復(fù)制代碼 代碼如下:

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

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


以上可知:

1.對于字符串,在{}定義時,可以key = value, 也可以["flag"] = nil,索引都是string類型,對于非nil類型變量(包括字符串),都可以[variable]=value的方式
2.使用table時,對于字符串,可以通過.的方式訪問,也可以通過[]方式訪問。tab[a],tab[b],只要a==b那么tab[a]可以訪問到tab[b]的值
3.不管定義索引時用的是常量還是變量,最終table中value的索引key是常量,不會隨變量的改變而變化該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庫函數(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())

(完)

您可能感興趣的文章:
  • Lua中對table排序?qū)嵗?/li>
  • Lua中table的一些輔助函數(shù)介紹
  • Lua table簡明總結(jié)
  • Lua中的metatable詳解
  • Lua中table庫函數(shù)方法介紹
  • Lua中的table學(xué)習(xí)筆記

標(biāo)簽:金昌 德宏 天門 宣城 濰坊 臺灣 天門 儋州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Lua table類型學(xué)習(xí)筆記》,本文關(guān)鍵詞  Lua,table,類型,學(xué)習(xí),筆記,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Lua table類型學(xué)習(xí)筆記》相關(guān)的同類信息!
  • 本頁收集關(guān)于Lua table類型學(xué)習(xí)筆記的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    深夜做爰性大片中文| 99热精品在线| 91麻豆国产| 精品视频在线观看免费| 国产91精品系列在线观看| 国产91精品系列在线观看| 青青青草视频在线观看| 色综合久久天天综线观看| 免费毛片基地| 久久福利影视| 高清一级毛片一本到免费观看| 国产伦理精品| 亚欧成人乱码一区二区| 精品久久久久久综合网| 免费一级生活片| 久草免费在线观看| 精品国产一区二区三区精东影业 | 99久久精品国产麻豆| 免费毛片基地| 国产综合91天堂亚洲国产| 美女免费毛片| 韩国三级一区| 精品国产三级a| 国产网站免费视频| 麻豆系列 在线视频| 国产精品自拍在线观看| 夜夜操网| 免费国产在线视频| 免费国产在线观看| 91麻豆国产| 九九干| 午夜在线亚洲| 精品视频在线观看一区二区| 国产a毛片| 色综合久久天天综线观看| 韩国三级香港三级日本三级| 黄视频网站免费看| 成人a大片在线观看| 国产激情一区二区三区| 国产精品1024在线永久免费| 一级女人毛片人一女人| 久久99中文字幕久久| 欧美电影免费| 色综合久久天天综线观看| 国产麻豆精品免费视频| 91麻豆精品国产自产在线观看一区| 日日夜人人澡人人澡人人看免| 99久久精品国产免费| 国产一区二区高清视频| 99久久网站| 九九精品在线播放| 国产福利免费视频| 91麻豆精品国产自产在线观看一区 | 99久久精品国产国产毛片 | 成人免费观看的视频黄页| 精品国产一区二区三区国产馆| 欧美一区二区三区性| 日本在线www| 亚欧乱色一区二区三区| 日韩在线观看视频免费| 国产不卡在线观看视频| 97视频免费在线观看| 欧美a级片免费看| 999久久狠狠免费精品| 91麻豆精品国产自产在线观看一区 | 成人免费一级毛片在线播放视频| 99热精品在线| 亚洲天堂在线播放| 欧美激情一区二区三区在线播放| 九九精品在线播放| 精品久久久久久中文字幕2017| 欧美激情一区二区三区在线| 午夜精品国产自在现线拍| 青青久热| 欧美激情中文字幕一区二区| 日本在线www| 久久国产一区二区| 九九久久99| 欧美日本免费| 国产美女在线观看| 国产成a人片在线观看视频| 欧美a级片免费看| 国产麻豆精品| 成人高清视频免费观看| 国产成人精品综合| 午夜在线亚洲男人午在线| 国产极品精频在线观看| 香蕉视频三级| 日本免费区| 九九精品久久| 青草国产在线| 成人影视在线播放| 国产网站麻豆精品视频| 国产成人欧美一区二区三区的| 九九热国产视频| 日韩免费在线观看视频| 人人干人人草| 麻豆系列国产剧在线观看| 国产亚洲精品aaa大片| 精品国产一级毛片| 国产一区免费观看| 国产一区免费在线观看| 你懂的日韩| 九九久久99| 青青久久精品国产免费看| 久久精品免视看国产明星| 久久精品大片| 国产原创中文字幕| 日本在线不卡免费视频一区| 久草免费资源| 国产不卡在线观看视频| 四虎久久影院| 天天色色网| 你懂的在线观看视频| 一级片片| 欧美爱爱动态| 青草国产在线| 亚洲女人国产香蕉久久精品 | 99久久精品国产免费| 久久久久久久网| 日本在线www| 国产国产人免费视频成69堂| 精品久久久久久免费影院| 成人a级高清视频在线观看| 日本免费看视频| 精品视频在线观看免费| 国产视频在线免费观看| 国产高清视频免费观看| 欧美1卡一卡二卡三新区| 尤物视频网站在线观看| 你懂的日韩| 精品视频在线观看一区二区| 99色视频在线| 久草免费资源| 九九九在线视频| 黄色短视屏| 久久精品免视看国产明星| 日日爽天天| 欧美日本国产| 国产国产人免费视频成69堂| 国产成人欧美一区二区三区的| 九九国产| 亚欧成人毛片一区二区三区四区| 精品国产香蕉在线播出 | 九九热国产视频| 四虎影视库国产精品一区| 午夜久久网| 午夜久久网| 国产美女在线一区二区三区| 国产国产人免费视频成69堂| 免费国产一级特黄aa大片在线| 日韩免费在线视频| 一级女性全黄生活片免费| 国产激情一区二区三区| 国产一区二区精品尤物| 亚飞与亚基在线观看| 精品视频免费在线| 亚洲天堂在线播放| 91麻豆国产福利精品| 91麻豆精品国产自产在线观看一区 | 久久国产一久久高清| 国产一区二区精品久| 国产伦精品一区二区三区无广告| 欧美激情一区二区三区视频 | 欧美激情一区二区三区中文字幕| 亚洲精品久久久中文字| 日本伦理网站| 亚洲精品永久一区| 色综合久久久久综合体桃花网| 美女免费精品高清毛片在线视| 黄视频网站在线看| 成人免费观看网欧美片| 91麻豆爱豆果冻天美星空| 国产一区二区高清视频| 国产不卡高清在线观看视频| 欧美大片a一级毛片视频| 国产不卡在线观看视频| 亚洲女人国产香蕉久久精品 | 亚洲wwwwww| 青草国产在线观看| 尤物视频网站在线| 一a一级片| 美女免费精品高清毛片在线视| 国产精品123| 国产视频一区二区在线观看| 九九久久99综合一区二区| 精品国产一区二区三区免费| 青青久久精品国产免费看| 国产一级强片在线观看| 久久精品店| 欧美1卡一卡二卡三新区| 欧美激情一区二区三区中文字幕| 国产成人精品综合在线| 麻豆系列国产剧在线观看| 欧美电影免费看大全| 国产极品精频在线观看| 免费国产一级特黄aa大片在线| 日本特黄特色aaa大片免费| 国产麻豆精品| 成人影视在线播放|