婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > Lua table類型學習筆記

Lua table類型學習筆記

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

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

基本介紹

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

例如:

復制代碼 代碼如下:

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

復制代碼 代碼如下:

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

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

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

print(tab[temp]) -- 結果是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

嵌套

復制代碼 代碼如下:

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
復制代碼 代碼如下:

--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的易變性

復制代碼 代碼如下:

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

c,d = {},{};

print(c == d) -->false

table庫函數使用
-----------------------------------------------------------
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.

復制代碼 代碼如下:

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.

復制代碼 代碼如下:

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

復制代碼 代碼如下:

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

復制代碼 代碼如下:

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

復制代碼 代碼如下:

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

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

面向對象編程

復制代碼 代碼如下:

--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排序實例
  • Lua中table的一些輔助函數介紹
  • Lua table簡明總結
  • Lua中的metatable詳解
  • Lua中table庫函數方法介紹
  • Lua中的table學習筆記

標簽:金昌 德宏 天門 宣城 濰坊 臺灣 天門 儋州

巨人網絡通訊聲明:本文標題《Lua table類型學習筆記》,本文關鍵詞  Lua,table,類型,學習,筆記,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Lua table類型學習筆記》相關的同類信息!
  • 本頁收集關于Lua table類型學習筆記的相關信息資訊供網民參考!
  • 推薦文章
    婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av
    色先锋资源久久综合| 国产喷白浆一区二区三区| 成人精品国产一区二区4080| 国产在线精品国自产拍免费| 国产精品亚洲午夜一区二区三区 | 在线免费观看日本一区| 色噜噜狠狠成人网p站| 欧美亚洲动漫另类| 制服.丝袜.亚洲.中文.综合| 日韩精品综合一本久道在线视频| 精品剧情在线观看| 欧美激情在线观看视频免费| 国产精品少妇自拍| 亚洲精品视频在线| 午夜影院久久久| 国内精品免费**视频| 成人精品视频网站| 欧洲日韩一区二区三区| 日韩视频一区在线观看| 国产免费观看久久| 亚洲蜜臀av乱码久久精品蜜桃| 亚洲国产综合在线| 国产美女在线观看一区| 一本大道综合伊人精品热热| 9191久久久久久久久久久| 亚洲精品一区二区三区蜜桃下载 | 欧洲精品一区二区三区在线观看| 欧美日韩国产精品成人| 久久综合久久综合久久综合| 亚洲精品视频免费看| 青青草一区二区三区| 成人永久aaa| 7777精品伊人久久久大香线蕉的 | 久久男人中文字幕资源站| 中文字幕一区二区三区在线观看| 日韩精品亚洲一区二区三区免费| 丁香婷婷深情五月亚洲| 8v天堂国产在线一区二区| 国产精品国产三级国产aⅴ无密码| 亚洲高清在线精品| bt欧美亚洲午夜电影天堂| 91精品国产aⅴ一区二区| 亚洲视频在线观看一区| 精品无码三级在线观看视频 | 国产精品一区二区三区99| 欧美亚洲另类激情小说| 中文一区二区完整视频在线观看| 丝袜美腿高跟呻吟高潮一区| av中文字幕在线不卡| 精品国产露脸精彩对白| 日韩在线一二三区| 91在线观看污| 国产精品人成在线观看免费| 韩国理伦片一区二区三区在线播放| 欧美午夜在线观看| 中文字幕在线一区二区三区| 激情偷乱视频一区二区三区| 777亚洲妇女| 亚洲一区二区三区在线看| av不卡免费在线观看| 国产欧美精品一区二区色综合| 日韩av电影一区| 制服.丝袜.亚洲.另类.中文| 亚洲va国产天堂va久久en| 欧美影片第一页| 一区二区三区日韩精品视频| 色综合久久天天综合网| 亚洲欧美日韩国产中文在线| av日韩在线网站| 综合久久久久综合| 色88888久久久久久影院野外| 中文字幕一区二区三中文字幕| www.欧美色图| 中文字幕亚洲区| 91在线视频官网| 一区二区三区色| 欧美精品少妇一区二区三区| 三级不卡在线观看| 5858s免费视频成人| 免费在线观看一区| 精品日韩成人av| 成人免费看的视频| 亚洲黄色录像片| 欧美日韩二区三区| 激情国产一区二区| 欧美韩国日本综合| 91欧美一区二区| 午夜精品久久久久久不卡8050| 欧美一区二区三区视频免费| 日本aⅴ免费视频一区二区三区 | wwww国产精品欧美| 不卡一区二区三区四区| 亚洲一区二区三区视频在线播放| 91精品国产入口| 国产成人自拍网| 一区二区国产盗摄色噜噜| 欧美男同性恋视频网站| 激情综合网激情| 亚洲三级电影网站| 日韩三级高清在线| 成人精品视频网站| 首页亚洲欧美制服丝腿| 国产亚洲成av人在线观看导航| 91一区二区三区在线播放| 日韩电影在线免费看| 国产精品网友自拍| 欧美精品丝袜久久久中文字幕| 国产精品一线二线三线| 亚洲国产日日夜夜| 国产女人aaa级久久久级 | 全国精品久久少妇| 中文字幕中文字幕一区| 日韩欧美成人午夜| av中文字幕亚洲| 国内精品免费**视频| 亚洲国产精品自拍| 国产精品妹子av| 精品三级在线观看| 欧美理论在线播放| eeuss鲁片一区二区三区在线观看| 免费精品视频在线| 亚洲午夜影视影院在线观看| 国产情人综合久久777777| 91精品欧美福利在线观看| 91亚洲精华国产精华精华液| 国产一区在线精品| 天堂午夜影视日韩欧美一区二区| 日韩理论电影院| 国产清纯白嫩初高生在线观看91| 91精品在线麻豆| 欧美日韩一级片在线观看| 成人av在线网站| 成人妖精视频yjsp地址| 国产福利精品一区| 韩国一区二区三区| 久久国产精品99久久久久久老狼| 亚洲小说春色综合另类电影| 亚洲女与黑人做爰| 亚洲精品亚洲人成人网| 国产精品久99| 国产精品午夜久久| 国产精品国产三级国产三级人妇| 久久精品一区二区三区不卡| 久久嫩草精品久久久精品一| www久久精品| 国产欧美日韩三区| 国产精品不卡在线| 国产精品久久久久久久午夜片| 国产精品女同一区二区三区| 国产精品理伦片| 中文字幕综合网| 尤物视频一区二区| 亚洲综合男人的天堂| 亚洲福利视频一区| 日韩在线观看一区二区| 日本视频中文字幕一区二区三区| 免费的国产精品| 国内精品在线播放| 丁香婷婷综合激情五月色| 91碰在线视频| 欧美久久久一区| 精品国产3级a| 中文字幕一区二区日韩精品绯色| 亚洲色欲色欲www| 日韩中文字幕1| 国产乱码精品一品二品| 97精品久久久久中文字幕| 欧美性生活大片视频| 日韩欧美一区二区不卡| 国产日韩三级在线| 亚洲人成亚洲人成在线观看图片 | 欧美亚洲动漫精品| 欧美一二三区在线观看| 国产午夜亚洲精品不卡| 夜夜嗨av一区二区三区网页| 日本vs亚洲vs韩国一区三区 | 亚洲国产精品一区二区久久| 六月丁香综合在线视频| jiyouzz国产精品久久| 欧美乱妇一区二区三区不卡视频| 久久久久国产精品免费免费搜索| 亚洲日本丝袜连裤袜办公室| 蜜桃传媒麻豆第一区在线观看| 成人激情开心网| 日韩欧美在线网站| 亚洲美女淫视频| 青青青爽久久午夜综合久久午夜| 福利电影一区二区| 欧美一级艳片视频免费观看| 亚洲三级小视频| 国产麻豆午夜三级精品| 欧美日韩一区 二区 三区 久久精品 | 国产传媒一区在线| 欧美日韩小视频| 国产精品无码永久免费888| 免费成人在线视频观看| 91久久精品午夜一区二区| 欧美国产国产综合| 国产一区二区影院| 日韩免费一区二区三区在线播放|