Lua 之table的测试
2015-07-06 10:21
459 查看
一.lua 的 table构造
1.字符串作为table索引法1.
MyTable = {x = "xixi"}
法2.
MyTable = {["x"] = "xixi"}
法3.
MyTable = {}
MyTable.x = "xixi"
法4.
MyTable = {}
MyTable["x"] = "xixi"
注:
当使用数字字符串作为索引的时候(如:“10”):
法1(错误):MyTable = {10 = "xixi"}
法2:MyTable = {["10"] = "xixi"]}
法3(错误):MyTable = {} MyTable.10 = "xixi"
法4:MyTable = {} MyTable["10"] = "xixi"
2.数字作为table索引
法1.
MyTable = {[2] = "xixi"}
法2.
MyTable = {}
MyTable[2] = "xixi"
法3.
MyTable = {"x","y","z"}
这时候默认起始坐标为1,所以值为"y"对应的是MyTable[2]
3.字符串作为table索引,值为函数
法1.
x = {func =
function ()
print("x func call")
end
}
法2.
y = {["func"] =
function ()
print("y func call")
end
}
法3.
z = {}
z.func =
function ()
print("z func call")
end
法4.
w = {}
w["func"] =
function ()
print("w func call")
end
法5.(新增定义方法)
m = {}
function m.func()
print("m func call")
end
二.访问table的方法
法1.MyTable[index]
法2.
MyTable.index
三.混合记录和列表风格的table构造式
polyline = {color = "blue",thickness = 2, "test",{x=0,y=0},
{x=-10,y=10}
}
print(polyline.color) --blue
print(polyline[1]) --test
print(polyline[2].x) --0
相关文章推荐
- luasocket系列: luasocket hello world!
- 在visual studio 2013中编译Lua5.3.1
- Lua53
- Lintcode - Expression Evaluation
- tolua++实现分析
- Lua脚本语法说明(转)
- Lua的特点
- Lua学习
- HDU 1202 The cacluation of GPA
- 图像检索评估准则——Evaluation Criteria of Image Retrieval : Retrieval Accuracy
- 【本·伍德Lua专栏】补充的基础06:简单的错误处理
- notepad++ 查找引用(Find Reference)(适用于c c++及各类脚本比如lua、python等)
- tolua 有些功能可以用(经过测试)
- Lua的线程和状态
- luasocket系列之winsocket基础补充—winsock库1
- Ngx_Lua使用分享
- ngx映射到lua模块函数变量一览
- Lua移植到arm上 并实现在arm上 可以让lua脚本调c语言,C语言调用lua脚本
- Weka开发[3]-Evaluation类
- Lua与C的交互