关系表类型,这是一个很强大的类型。我们可以把这个类型看作是一个数组。只是 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[])
 
以上写法都是对的。http://hovertree.com/hovertreescj/
 
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的易变性
 
= {}; b = a;
print(== b)  --> true
 
c,= {},{};
 
print(== 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) < string.sub(b,,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=,#table do
         print(i,table [])
    end
end
print("before insert:" )
printt(foo)
table.insert(foo,,"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 = {,2, 3,4,,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 = {[-]=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 ()
        n = n +;
    end
    return {get = get, inc= inc}
end
 
object = mg1(50 )
print(object.get ())
print(object["get" ]())
 
object.inc()
print(object.get ())
----------------------------------------
do
    local function get ()
         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
= mg3(50 )
a:get()
a.inc(a,)
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 , get =T.get , inc =T.inc }
    end
end
 
= mg4(30 )
print(c:get())
c:inc()
print(c:get())
 

lua学习之table类型的更多相关文章

  1. lua学习之类型与值篇

    类型与值 lua 是动态类型的语言 在语言中没有类型定义的语法 每个值都携带有它的类型信息 8种基础类型 用 type 可以返回这个值的类型的名称 将一个变量用于不同类型,通常会导致混乱的代码 但合理 ...

  2. Lua 学习笔记(二)语法、类型、值

    首先Lua执行的每一段代码都称之为“程序块”,一个程序块也就是一连串的语句或命令,例如一个源码文件或一行代码.Lua语句之间并不需要分隔符,如代码中的换行就不起任何作用,当然为了养成编码习惯当两条或者 ...

  3. lua中打印所以类型功能实现table嵌套table

    lua中打印所以类型功能实现 本人測试 number.string.bool.nil.table嵌套table.userdata没问题 共享一下有什么问题请拍砖 代码例如以下 cclog = func ...

  4. Lua学习(1)——table

    table类型实现了“关联数组”.“关联数组”是一种具有特殊索引方式的数组.不仅可以通过证书来索引它,还可以使用字符串或其他类型(除了nil)来索引它.table是Lua中主要的数据结构机制(事实也是 ...

  5. Lua学习之类型与值

    Lua是一种动态语言,在语言中没有类型定义的语法. 在lua中有8中基本的类型: 1.nil(空) 2.boolean 3.number(数字) 4.string(字符串) 5.userdata(自定 ...

  6. lua学习项目笔记

    这几天草草的浏览了一下电子版的<lua程序设计>,没有懂的地方就自动忽略了,挑拣了一些可以理解的部分一直在推进.推进至后面的时候已经浑浑噩噩的了,有种想看完这本书的强迫症的感觉.推进CAP ...

  7. [转]LUA 学习笔记

    Lua 学习笔记 入门级 一.环境配置 方式一: 1.资源下载http://www.lua.org/download.html 2.用src中的源码创建了一个工程,注释调luac.c中main函数,生 ...

  8. Lua学习笔记(二):基本语法

    Lua学习指南:http://www.lua.org/manual/ 首先我们要明确的一点是:在Lua中,除了关键字外一切都是变量. Lua关键字 可以查看这个地址:http://www.lua.or ...

  9. Lua 学习笔记(一)

    Lua学习笔记 1.lua的优势 a.可扩张性     b.简单     c.高效率     d.和平台无关 2.注释 a.单行注释 --        b.多行注释 --[[  --]] 3.类型和 ...

随机推荐

  1. 【原创】我是怎么从零开始教女同学进行php开发的(4)

    周末给自己放了一个小假,周五晚上跟同学出去吃饭,周六又休息了一天,直到周日才坐到电脑前面码字. 本来说好周末这两天把之前三篇的代码根据评论中的建议好好修改一下的,顺便认真系统地学习一遍HTML基础.结 ...

  2. 跟我一起云计算(6)——openAPI

    介绍 Open API即开放API,也称开放平台. 所谓的开放API(OpenAPI)是服务型网站常见的一种应用,网站的服务商将自己的网站服务封装成一系列API(Application Program ...

  3. IT传统组织结构及新型扁平化组织

    如今互联网企业正凶猛的改变人们衣食住行的方方面面,衣->淘宝,蘑菇街;食->大众点评,口碑;住->去哪,途牛:行->12306, 多次听到互联网的同行介绍他们就是要“快”,快速 ...

  4. bower使用记录

    每次做项目的时候都不依赖某一个库来开发,每次需要某一个库的时候都是百度进入库官网再找到下载的库,经常会因为官网的改版更新而在里面绕半天找不到想要的版本号,当然直接去github,CDN 都可以找到需要 ...

  5. h5原生拖拽

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. fir.im Weekly - 进击的 Swift

    最近 Swift 开源了,众开发者们欢呼雀跃.感谢开源,这是最好的时代.本期 fir.im Weekly 准备了一些关于 Swift 的"新鲜"干货分享,也包括一些优秀的 GitH ...

  7. iOS-MVC模式

    提到ios中的mvc不得不提2011秋季斯坦福课程的老头,他的iphone开发公开课是所有描述ios中mvc模式最为准确并且最为浅显易懂的. 模型-视图-控制器 这个模式其实应该叫做MCV,用控制器把 ...

  8. DOM_05之DOM、BOM常用对象

    1.HTML DOM常用对象之Table:①创建:createTHead():createTBody():createTFoot():②删除:deleteTHead():deleteTFoot():③ ...

  9. 用例设计工具PICT — 输入组合覆盖

    1 成对测试简介 成对测试(Pairwise Testing)又称结对测试.两两测试,是一种正交分析的测试技术.成对组合覆盖这一概念是Mandl于1985年在测试Aad编译程序时提出来的.是当不可能遍 ...

  10. 快速入门系列--MVC--07与HTML5移动开发的结合

    现在移动互联网的盛行,跨平台并兼容不同设备的HTML5越来越盛行,很多公司都在将自己过去的非HTML5网站应用渐进式的转化为HTML5应用,使得一套代码可以兼容不同的物理终端设备和浏览器,极大的提高了 ...