最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Lua table類型學習筆記

 更新時間:2015年04月23日 10:08:41   投稿:junjie  
這篇文章主要介紹了Lua table類型學習筆記,本文講解了table的基礎(chǔ)知識和table庫函數(shù)的使用以及面向?qū)ο缶幊虒嵗?需要的朋友可以參考下

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

基本介紹

注意三點:
    第一,所有元素之間,總是用逗號 "," 隔開;
    第二,所有索引值都需要用 "["和"]" 括起來;如果是字符串,還可以去掉引號和中括號; 即如果沒有[]括起,則認為是字符串索引
    第三,如果不寫索引,則索引就會被認為是數(shù)字,并按順序自動從 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]) --此時的結(jié)果是11,因為11沒有顯式對應(yīng)的key,因此從1開始,如果前面定義了,則覆蓋其value
復制代碼 代碼如下:

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

嵌套

復制代碼 代碼如下:

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庫函數(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.

復制代碼 代碼如下:

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

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

復制代碼 代碼如下:

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

(完)

相關(guān)文章

  • lua+love2d制作的2048游戲

    lua+love2d制作的2048游戲

    前面給大家分享的是一個超級簡單版的使用lua實現(xiàn)的2048小游戲的代碼,今天我們加上love2d游戲引擎,制作PC版的2048游戲。小伙伴們仔細讀讀本文吧。
    2015-03-03
  • Lua之字符串格式化例子和常用格式化參數(shù)介紹

    Lua之字符串格式化例子和常用格式化參數(shù)介紹

    這篇文章主要介紹了Lua之字符串格式化例子和常用格式化參數(shù)介紹,本文著重講解了格式化參數(shù)的作用,需要的朋友可以參考下
    2015-04-04
  • Lua面向?qū)ο缶幊虒W習筆記

    Lua面向?qū)ο缶幊虒W習筆記

    這篇文章主要介紹了Lua面向?qū)ο缶幊虒W習筆記,本文講解了Lua中實現(xiàn)類的例子、類之間繼承的例子等內(nèi)容,需要的朋友可以參考下
    2014-12-12
  • 用sysbench來測試MySQL的性能的教程

    用sysbench來測試MySQL的性能的教程

    這篇文章主要介紹了用sysbench來測試MySQL的性能的教程,使用Lua腳本操作,需要的朋友可以參考下
    2015-04-04
  • Redis教程(五):Set數(shù)據(jù)類型

    Redis教程(五):Set數(shù)據(jù)類型

    這篇文章主要介紹了Redis教程(五):Set數(shù)據(jù)類型,本文講解了Set數(shù)據(jù)類型概述、相關(guān)命令、命令使用示例、應(yīng)用范圍等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • OpenResty中正則模式匹配的2種方法詳解

    OpenResty中正則模式匹配的2種方法詳解

    在 OpenResty 中,同時存在兩套正則表達式規(guī)范:Lua 語言的規(guī)范和 Nginx 的規(guī)范,下面這篇文章主要給大家介紹了關(guān)于OpenResty中正則模式匹配的2種方法,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下。
    2018-04-04
  • Lua多行注釋和取消多行注釋的方法

    Lua多行注釋和取消多行注釋的方法

    這篇文章主要介紹了Lua多行注釋和取消多行注釋的方法,本文分別給出代碼示例,請注意細節(jié)~,需要的朋友可以參考下
    2015-06-06
  • Lua中的操作符和表達式總結(jié)

    Lua中的操作符和表達式總結(jié)

    這篇文章主要介紹了Lua中的操作符和表達式總結(jié),本文總結(jié)了算術(shù)操作符、關(guān)系操作符、邏輯操作符、字符串連接、table構(gòu)造式等,需要的朋友可以參考下
    2014-09-09
  • Lua中的string庫和強大的模式匹配學習筆記

    Lua中的string庫和強大的模式匹配學習筆記

    這篇文章主要介紹了Lua中的string庫和強大的模式匹配學習筆記,本文著重總結(jié)了string庫的一些操作方法和函數(shù),需要的朋友可以參考下
    2015-04-04
  • Lua基本語法

    Lua基本語法

    Lua是相當簡單易學,本篇文章來給大家稍微講一下Lua的語法,不會長篇累牘得把Lua的所有語法都講一遍,這里通過以下幾點來講Lua語言的基礎(chǔ)語法。
    2015-05-05

最新評論

保德县| 通州区| 湄潭县| 元江| 托里县| 山东省| 蓬安县| 稷山县| 康乐县| 缙云县| 尼玛县| 中宁县| 抚远县| 清远市| 永州市| 射洪县| 宝鸡市| 宜君县| 施甸县| 华阴市| 普宁市| 洮南市| 义乌市| 北安市| 五常市| 平湖市| 拜泉县| 星座| 安塞县| 弋阳县| 青阳县| 阿鲁科尔沁旗| 内乡县| 嘉祥县| 盘锦市| 长子县| 航空| 隆子县| 鄄城县| 长丰县| 临邑县|