lua table 的操作(四)
table在前面作过介绍,它是一种关联数组,这种关联指的是可以设置各类类型的key来存储值。
1.table 间的数据传递
-- 为 table a 并设置元素,然后将 a 赋值给 b,则 a 与 b 都指向同一个内存地址
-- 如果 a 设置为 nil ,则 b 同样能访问 table 的元素。
-- 如果没有指定的变量指向a,Lua的垃圾回收机制会清理相对应的内存。
mytable = {};
print("mytable的类型是:",type(mytable)); mytable[] = "lua"
mytable["wow"] = "修改前的值" print("mytable 索引为1的元素是:",mytable[])
print("mytable 索引为wow的元素是:",mytable["wow"]) beforetable = mytable;
print("beforetable 索引为1的元素是:",beforetable[])
print("mytable 索引为wow的元素是:",mytable["wow"])
beforetable["wow"] = "修改后的值"
print("mytable 索引为wow的元素是:",mytable["wow"]) -- 释放变量
beforetable = nil;
print("beforetable是:",beforetable) -- mytable 仍然可以访问
print("mytable索引为wow的元素是:",mytable["wow"])
mytable = nil;
print("mytable是:",mytable)
2.table的操作
-- table 的操作:
-- 1.连接 table.concat()
-- 2.插入 table.insert();在数据的指定位置插入元素,默认在末尾
-- 3.移除 table.remove(table,pos) 移除指定位置的元素
-- 4.排序 table.sort(table) fruits = {"banana","orange","apple"} --- 连接操作
print("连接后的字符串是:",table.concat(fruits));
-- 指定符号连接
print("连接后的字符串是:",table.concat(fruits,", "));
-- 指定索引连接
print("连接后的字符串是:",table.concat(fruits,", ",,));
--- 插入操作
table.insert(fruits,,"pear")
print("插入后的元素是:",fruits[]) --- 移除操作
print("移除前是:",table.concat(fruits,", "));
table.remove(fruits,);
print("移除前后:",table.concat(fruits,", "));
--- 排序操作
print("排序前:")
for i, v in pairs(fruits) do
print(i,v)
end table.sort(fruits)
print("排序后:")
for i, v in pairs(fruits) do
print(i,v)
end
lua table 的操作(四)的更多相关文章
- Lua 学习之基础篇四<Lua table(表)>
table 是 Lua 的一种数据结构用来帮助我们创建不同的数据类型,如:数组.字典等. Lua table 使用关联型数组,你可以用任意类型的值来作数组的索引,但这个值不能是 nil. Lua ta ...
- lua table remove元素的问题
当我在工作中使用lua进行开发时,发现在lua中有4种方式遍历一个table,当然,从本质上来说其实都一样,只是形式不同,这四种方式分别是: for key, value in pairs(tbtes ...
- lua table长度解析
先来看lua table源码长度获取部分(ltable.c) j是数组部分的长度.首先判断数组长度大于0,并且数组最后一个是nil,就用二分法查找,返回长度. 如果t->node是 table的 ...
- Lua table(表)
table 是 Lua 的一种数据结构用来帮助我们创建不同的数据类型,如:数组.字典等. Lua table 使用关联型数组,你可以用任意类型的值来作数组的索引,但这个值不能是 nil. Lua ta ...
- 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 ...
- lua table 排序--满足多条件排序
前提 假设 一个小怪 有三种属性,等级(level).品质(quality).id(pid) 我们需要对他们进行排序,两种排序情况,第一是单一属性排序,比如按照等级进行排序,或者多种属性进行优先级排序 ...
- cocos2d-x lua table数据存储
cocos2d-x lua table数据存储 version: cocos2d-x 3.6 1. 将table转为json http://blog.csdn.net/songcf_faith/art ...
- cocos2d-x lua table与json的转换
cocos2d-x lua table与json的转换 version: cocos2d-x 3.6 1.引入json库 require("src/cocos/cocos2d/json&qu ...
随机推荐
- java+web+多级文件上传
文件夹数据库处理逻辑 publicclass DbFolder { JSONObject root; public DbFolder() { this.root = new JSONObject(); ...
- [Luogu] 余数求和
question: $$\sum_{i=1}^{n} k \bmod i$$$$\sum_{i=1}^{n} k - \lfloor \frac{k}{i} \rfloor i$$$$\sum_{i= ...
- 全局变量异步I/O
/*** sync_process.c ***/ #include <stdio.h> #include <signal.h> #include <unistd.h> ...
- Appium获取toast消息
Android获取toast,需要在参数里设置automationName:Uiautomator2 设置设备的信息 desired_caps = { 'platformName': 'Android ...
- JVM——类加载
一.什么是类加载? JVM将class字节码文件加载到内存中, 并将这些静态数据转换成方法区中的运行时数据结构,在堆中生成一个代表这个类的java.lang.Class 对象,作为方法区类数据的访问入 ...
- 3-2新建Photoshop图像
http://www.missyuan.com/thread-350740-1-1.html [CTRL N][文件 新建] 按住CTRL双击Photoshop的空白区(这个好像是打开文件){快捷 ...
- SNMP 协议介绍 转载
一.SNMP简单概述 1.1.什么是Snmp SNMP是英文"Simple Network Management Protocol"的缩写,中文意思是"简单网络管理协议& ...
- final关键字的理解
final :最终作为一个修饰符 1.可以修饰类,函数,变量: 2.被final修饰的类不可以被继承: 3.被final修饰的方法不可以被复写: 4.被final修饰的变量是一个常量,只能赋值一次,既 ...
- CF1208C
CF1208C 这场杜老师大战tourist的比赛怎么这么多人类智慧题... 题意: 构造一个 $ n \times n $ 的矩阵,使得该矩阵每一行与每一列的元素的异或和全部相等. 解法: 异或的神 ...
- open suse tumbleweed安装记录
zypper install imagewriter cmake blender fontforge gimp digikam inkscape kontact pitivi smplayer si ...