Lua中Table的学习
--table 是 Lua 的一种数据结构,用来帮助我们创建不同的数据类型,如:数组、字典等
--Lua也是通过table来解决模块(module)、包(package)和对象(Object)的。 例如string.format表示使用"format"来索引table string。 mytable = {}
print("mytable 的类型:",type(mytable)) mytable[] = "Lua"
mytable["wow"] = "修改前"
print("mytable 索引为 1 的元素:",mytable[])
print("mytable 索引为 wow 的元素:",mytable["wow"]) --两个table指向的是同一块内存
lgstable = mytable print("lgstable 索引为 1 的元素:",lgstable[]) print("mytable 索引为 wow 的元素:",mytable["wow"])
lgstable["wow"] = "修改后"
print("mytable 索引为 wow 的元素:",mytable["wow"]) --释放变量
lgstable = nil
print("lgstable :",lgstable) --mytable依然可以访问
print("mytable 索引为 wow 的元素:",mytable["wow"]) mytable = nil
print("mytable :",mytable) --[[
以上代码执行结果: mytable 的类型: table
mytable 索引为 1 的元素: Lua
mytable 索引为 wow 的元素: 修改前
lgstable 索引为 1 的元素: Lua
mytable 索引为 wow 的元素: 修改前
mytable 索引为 wow 的元素: 修改后
lgstable : nil
mytable 索引为 wow 的元素: 修改后
mytable : nil
--]] --table的操作 --table连接表中的元素为字符串
fruits = {"banana","orange","apple"}
--返回table连接后的字符串
print(table.concat(fruits))
--指定连接符
print(table.concat(fruits,','))
--指定索引
print(table.concat(fruits,',',,)) --[[
运行结果:
bananaorangeapple
banana,orange,apple
orange,apple
--]] --插入和移除
fruits2 = {"banana","orange","apple"}
--末尾插入
table.insert(fruits2,'mango')
print(fruits2[])
--索引为2的键处插入
table.insert(fruits2,,'grapes')
print(fruits2[]) print(fruits2[])
table.remove(fruits2)
print(fruits2[]) --[[
运行结果:
mango
grapes
mango
nil
--]] --table排序
fruits3 = {"banana","orange","apple", "grapes"}
print("排序前")
for i,v in ipairs(fruits3) do
print(i,v)
end table.sort(fruits3) print("排序后")
for i,v in ipairs(fruits3) do
print(i,v)
end --获取table的长度
print(#fruits3)
print(table.getn(fruits3))
以下为table的补充内容:
table.insert(t, pos, value) 在t指定位置插入元素
table.insert(t, value) 在t末尾插入元素
table.remove(t, pos) 在t指定位置删除元素,并返回删除的元素
table.remove(t) 在t末尾删除元素,并返回删除的元素
a = {};
table.insert(a,, )
table.insert(a,, )
table.insert(a,)
for i,v in ipairs(a) do
print(i,v)
end
print("-----------");
b = table.remove(a, );
print("b = " .. b);
table.remove(a);
for i,v in ipairs(a) do
print(i,v)
end
输出结果:

Lua中Table的学习的更多相关文章
- lua中 table 元表中元方法的重构实现
转载请标明出处http://www.cnblogs.com/zblade/ lua作为游戏的热更新首选的脚本,其优势不再过多的赘述.今天,我主要写一下如何重写lua中的元方法,通过自己的重写来实现对l ...
- lua中 table 重构index/pairs元方法优化table内存占用
转载请标明出处http://www.cnblogs.com/zblade/ lua作为游戏的热更新首选的脚本,其优势不再过多的赘述.今天,我主要写一下如何重写lua中的元方法,通过自己的重写来实现对l ...
- lua中 table.getn(t) 、#t、 table.maxn(t) 这三个什么区别?
lua中 table.getn(t) .#t. table.maxn(t) 这三个什么区别? RTlocal t = {1,888,x= 999,b=2,5,nil,6,7,[10]=1,8,{z = ...
- Lua中table的实现-《Lua设计与实现》
本文来自<Lua设计与实现>的阅读笔记,推荐Lua学习者可以购买一本,深入浅出讲解lua的设计和实现原理,很赞,哈哈 Lua中对于表的设计,是基于数组和散列表,和其他语言不同,对于数组 ...
- lua中table的遍历,以及删除
Lua 内table遍历 在lua中有4种方式遍历一个table,当然,从本质上来说其实都一样,只是形式不同,这四种方式分别是: 1. ipairs for index, value in ipair ...
- lua中table如何安全移除元素
在Lua中,table如何安全的移除元素这点挺重要,因为如果不小心,会没有正确的移除,造成内存泄漏. 引子 比如有些朋友常常这么做,大家看有啥问题 将test表中的偶数移除掉local test = ...
- C++对Lua中table进行读取、修改和创建
C++代码: // LuaAndC.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #i ...
- Lua中元表的学习
--table 中我们可以访问对应的key来得到value值,但是却无法对两个 table 进行操作. --元表(Metatable),允许我们改变table的行为,可以对两个table进行操作 -- ...
- Lua中数组的学习
--数组的大小是不固定的 --一维数组的逻辑结构是线性表索引从1开始 array1 = {"Lua", "Tutorial"} , do print(array ...
随机推荐
- 017-linux正则表达式
一.单字符表示:1.特定字符:某个具体的字符. '1' 'a' '\.'2.范围内单个字符:单个字符[] [0-9] [259] [a-z] [abc] [A-Z] [ABC] [a-zA-Z] [, ...
- 分布式一致性Hash
转载: https://blog.csdn.net/bntX2jSQfEHy7/article/details/79549368 为什么要有Hash一致性算法?就像以前介绍为什么要有Spring一样, ...
- [转]Mac Appium环境安装
原文:https://blog.csdn.net/dongqiushan/article/details/53326518 1.安装JDK; 2.安装Android SDK; 3.安装brew; 4. ...
- 限制 input 输入框只能输入纯数字
oninput = "value=value.replace(/[^\d]/g,'')"
- IO(字节流)
1. 字节流类以InputStream 和 OutputStream为顶层类,他们都是抽象类(abstract) 2. 最重要的两种方法是read()和write(),它们分别对数据的字节进行读写.两 ...
- Linux基础命令---mktemp
mktemp 创建临时文件或者目录,这样的创建方式是安全的.此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. 1.语法 mk ...
- QQ在线客服,默认到要加好友,授权也不起作用需要先开通QQ营销服务
QQ在线客服,默认到要加好友,授权也不起作用需要先开通QQ营销服务http://wpa.qq.com/msgrd?v=3&uin=你的客服QQ号码&site=qq&menu=y ...
- 2016NOI冬令营day0
上午写了一道题. 中午收拾好东西后兴高采烈地坐老司机开的出租车来到了美丽的南山中学!!!:) 志愿者太多了,弄得我们有点不好意思......:) 来到寝室,看到了传闻中的被子&空(kong4) ...
- bzoj 1179 [APIO 2009]Atm(APIO水题) - Tarjan - spfa
Input 第一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路的起点和终点的路口编号.接下来N行,每行一 ...
- How do I update a GitHub forked repository?
I recently forked a project and applied several fixes. I then created a pull request which was then ...