關系表類型,這是一個很強大的類型。我們可以把這個類型看作是一個數組。只是 C語言的數組,只能用正整數來作索引; 在Lua中,你可以用任意類型的值來作數組的索引,但這個值不能是 nil。同樣,在C語言中,數組的內容只允許一種類型;在 Lua中,你也可以用任意類型的值來作數組的內容,nil也可以。
基本介紹
注意三點:
第一,所有元素之間,總是用逗號 "," 隔開;
第二,所有索引值都需要用 "["和"]" 括起來;如果是字符串,還可以去掉引號和中括號; 即如果沒有[]括起,則認為是字符串索引
第三,如果不寫索引,則索引就會被認為是數字,并按順序自動從 1往后編;
例如:
print(tab[tt])
print(tab.key)
print(tab[1 ])
look = {[www] = "ok"}這樣是不對的,www沒有賦值,所以默認為nil因此出錯table index is nil
以上可知:
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
嵌套
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的易變性
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.
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.
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.
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.
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
duck = {[-2 ]=3,[- 1]=0}
print(table.maxn (duck )) -- 0
面向對象編程
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())
(完)
新聞熱點
疑難解答