关系表类型,这是一个很强大的类型。我们可以把这个类型看作是一个数组。只是C语言的数组,只能用正整数来作索引;在Lua中,你可以用任意类型的值来作数组的索引,但这个值不能是nil。同样,在C语言中,数组的内容只允许一种类型;在Lua中,你也可以用任意类型的值来作数组的内容,nil也可以。基本介绍注意三点: 第一,所有元素之间,总是用逗号”,”隔开; 第二,所有索引值都需要用”[“和”]”括起来;如果是字符串,还可以去掉引号和中括号;即如果没有[]括起,则认为是字符串索引 第三,如果不写索引,则索引就会被认为是数字,并按顺序自动从1往后编;例如:tt={“hello”,33}value=4tab={[tt]=”table”,key=value,[“flag”]=nil,11}print(tab[tt])print(tab.key)print(tab[1])以上写法都是对的。http://hovertree.com/hovertreescj/look = {[www] = “ok”}这样是不对的,www没有赋值,所以默认为nil因此出错table index is nil—temp = 1tab = {[temp] = 1, 11}print(tab[temp]) –此时的结果是11,因为11没有显式对应的key,因此从1开始,如果前面定义了,则覆盖其value—temp = 2tab = {[temp] = 1, 11}temp = 1print(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 33print(tab11.tb12[“bool”])–same as line 33print(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)–> truec,d={},{};print(c==d)–>falsetable库函数使用———————————————————–1. table.sort (table [, comp])
Sorts table elements in a given order,in-place, fromtable[1]
totable[n]
, wheren
is the length of the table. Ifcomp
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 thatnot comp(a[i+1],a[i])
will be true after the sort). Ifcomp
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)fork,vinipairs(name)doprint(k,v)end–table.sort uses callbacks. a function that is writtent to be called by a library function.functioncmp(a,b)ifstring.sub(a,2,2)<string.sub(b,2,2)thenreturntrueelsereturnfalseendendtable.sort(name,cmp)fork,vinipairs(name)doprint(k,v)end2. table.insert (table, [pos,] value)
Inserts elementvalue
at positionpos
intable
, shifting up other elements to open space, if necessary. The default value forpos
isn+1
, wheren
is the length of the table so that a calltable.insert(t,x)
insertsx
at the end of tablet
.–table.insert –an easy to copy a table to another table or adding elements to an array.!foo={“a”,”c”,”d”}bar={}functionprintt(table)fori=1,#tabledoprint(i,table[i])endendprint(“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 forsep
is the empty string, the default fori
is 1, and the default forj
is the length of the table. Ifi
is greater thanj
, 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 fromtable
the element at positionpos
, shifting down other elements to close the space, if necessary. Returns the value of the removed element. The default value forpos
isn
, wheren
is the length of the table, so that a calltable.remove(t)
removes the last element of tablet
.
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.maxnapple={“a”,”p”,[5]=”e”}print(table.maxn(apple))– 5duck={[-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.functionmg1(n)localfunctionget()returnn;endlocalfunctioninc(m)n=n+m;endreturn{get=get,inc=inc}endobject=mg1(50)print(object.get())print(object[“get”]())object.inc(2)print(object.get())—————————————-dolocalfunctionget(o)returno.oneendlocalfunctioninc(self,two)self.one=self.one+twoendfunctionmg3(one)return{one=one,get=get,inc=inc}endenda=mg3(50)a:get()a.inc(a,2)print(a:get())—————————————-dolocalT={};functionT:get()returnself.n;endfunctionT:inc(m)self.n=self.n+m;endfunctionmg4(n)return{n=n,get=T.get,inc=T.inc}endendc=mg4(30)print(c:get())c:inc(4)print(c:get())推荐:http://www.cnblogs.com/roucheng/p/lua1.html真正的强者,不是流泪的人,而是含泪奔跑的人。