Lua Table 操作
Lua中table类似与C#种的字典,其实就是一个key-value键值对数据结构.来学习下table基本操作
Table的创建
myTable = {} --表名后面使用{}赋值,表示一个空的表
myTable = {name="盘子脸",age=18,isMan=true} --创建时候就添加键-值
myTable = {10,20,30,"plateface"} --创建数字下标值的table,默认是从1开始
Table的赋值
myTable[3] = 34 --当键是一个数字的时候的赋值方式
myTable["name"] = "盘子脸" --当键是一个字符串的赋值方式
myTable.name = "plateface" --跟myTable["name"]访问的是同一个value, print(myTable.name) 输出plateface
Table的访问
myTable[3] --当键是数字的时候,只有一种访问方式
myTable.name --当键是字符串的时候有两种访问方式
myTable["name"]
Table的遍历
myTable = {10,20,30,40}
for index=1,table.getn(myTable) do
print(myTable[index])
end for index,value in ipairs(myTable) do
print(index,value)
end
表相关的函数:
table.conccat | 把表中所有数据连成一个字符串 |
table.insert | 在表中2的位置插入一个 |
table.remove | 移除指定位置的数据 |
table.sort | 排序 |
通过表来实现面向对象(一)
Enemy = {}
local this = Enemy --定义属性
Enemy.hp = 100
Enemy.speed = 12.3 --定义方法
Enemy.Move = function()
print("敌人在移动")
end function Enemy.Attack()
print(this.hp," 敌人的HP")
this.Move()
end --调用攻击方法
Enemy.Attack()
通过表来实现面向对象(二)
Apple = {} --使用:给table添加方法
function Apple:new()
Apple.str = "苹果的价格是6元"
return Apple;
end function Apple:toString()
print(Apple.str)
end local a = Apple:new()
print(a.toString())
Lua Table 操作的更多相关文章
- lua table操作实例详解
lua_gettable lua_getglobal(L, "mytable") <== push mytable lua_pushnumber(L, 1) & ...
- lua table操作
求最大值,最小值及长度: function maxn(t) local mn = nil for i, v in pairs(t) do if (mn==nil) then mn=v end if ( ...
- lua -- table.nums
table.nums 计算表格包含的字段数量. 格式: count = table.nums(表格对象) Lua 的“#”操作可以取得表格的长度,但仅限从 开始连续数字为索引的表格.table.num ...
- openresty开发系列19--lua的table操作
openresty开发系列19--lua的table操作 Lua中table内部实际采用哈希表和数组分别保存键值对.普通值:下标从1开始 不推荐混合使用这两种赋值方式. local color={fi ...
- Lua 学习之基础篇四<Lua table(表)>
table 是 Lua 的一种数据结构用来帮助我们创建不同的数据类型,如:数组.字典等. Lua table 使用关联型数组,你可以用任意类型的值来作数组的索引,但这个值不能是 nil. Lua ta ...
- Lua table(表)
table 是 Lua 的一种数据结构用来帮助我们创建不同的数据类型,如:数组.字典等. Lua table 使用关联型数组,你可以用任意类型的值来作数组的索引,但这个值不能是 nil. Lua ta ...
- Oracle索引梳理系列(十)- 直方图使用技巧及analyze table操作对直方图统计的影响(谨慎使用)
版权声明:本文发布于http://www.cnblogs.com/yumiko/,版权由Yumiko_sunny所有,欢迎转载.转载时,请在文章明显位置注明原文链接.若在未经作者同意的情况下,将本文内 ...
- lua table integer index 特性
table.maxn (table) Returns the largest positive numerical index of the given table, or zero if the t ...
- 树形打印lua table表
为方便调试lua程序,往往想以树的形式打印出一个table,以观其表内数据.以下罗列了三种种关于树形打印lua table的方法;法一 local print = print local tconca ...
随机推荐
- python wmi模块学习
# -*- coding: cp936 -*- import wmi c = wmi.WMI () for sys in c.Win32_OperatingSystem(): print " ...
- JavaScript 获取CSS媒体查询信息
var result = window.matchMedia('(max-width: 700px)'); if (result.matches) { console.log('页面宽度小于等于700 ...
- Makefile学习(三)执行make
9 执行make 一般方法:make. 某些情况:1.可能需要使用make更新一部分过时文件而不是全部 2.需要使用另外的编译器或者重新定义编译选项 3.只需要查看哪些文件被修改,不需要重新编译 所以 ...
- oracle 格式化数字 to_char
转:http://blog.csdn.net/chinarenzhou/article/details/5748965 Postgres 格式化函数提供一套有效的工具用于把各种数据类型(日期/时间,i ...
- Ubuntu中设置静态IP和DNS(转载)
原文地址:http://blog.sina.com.cn/s/blog_669421480102v3bb.html VMware 中使用网络,对虚拟机设置静态IP:在Ubuntu中设置静态IP共两步: ...
- CSS flex 布局 一些基本属性应用
作用于伸缩盒元素上的属性 box-orient .box-pack.box-align.box-direction.box-lines box-orient box-orient:horizontal ...
- redis批量执行
1.首先把redis命令放在txt文件中 eg: 文件名: test.txt 内容如下: HMSET HM_001 name zhang01 age HMSET HM_002 name zhang0 ...
- OpenGL ES 2.0 摄像机与投影
1.摄像机的设置 摄像机的位置坐标 摄像机的位置 摄像机up方向 Matrix.setLookAtM( mVMatrix, //存储生成矩阵元素的float[]类型数组 0, //填充起始偏移量 cx ...
- linux中的fork函数的基本用法
代码: #include <iostream> #include <string> #include <cstdio> #include <unistd.h& ...
- (原).cc 和 .cpp 后缀结尾的文件的区别
This caused a few problems the first time C++ was ported to a system where case wasn't significant i ...