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 ...
随机推荐
- 001_STM32程序移植之_DS1302
1. 测试环境:STM32C8T6 2. 测试模块:DS1302时钟模块 3. 测试接口: 1. DS1302模块接口: DS1302引脚 单片机引脚 VCC--------------------3 ...
- uname/hostname
uname 显示操作系统相关信息的命令. hostname 显示或者设置当前系统的主机名 修改hostname
- SSH远程连接排错的过程
SSH远程连接排错的过程 http://note.youdao.com/noteshare?id=bc81e9036cd2067cb1857ca9f54299a7 补充 ping命令用来测试主机之间网 ...
- 【概率论】4-4:距(Moments)
title: [概率论]4-4:距(Moments) categories: - Mathematic - Probability keywords: - Moments - Moments Gene ...
- scss的使用
看到一篇很好的文章,感觉不用我自己总结了, 上个链接:https://blog.csdn.net/zhouzuoluo/article/details/81010331
- 求ascii 然后做运算
介绍 ABC 返回每个字符的ascii A->65 B->66 C->77 组成656667 把所有的7替换成1 然后变成 656667 和 656661 每个数值做加法 然后做减法 ...
- zabbix监控windows案例
首先在zabbix官网下载zabbix软件包:https://www.zabbix.com/ 下载完成之后,将其解压到D盘 # 配置与安装,配置zabbix agent相关配置. 找到conf下的配置 ...
- Apache Flink - 命令
$flink命令位置 命令 选项 jar包位置 \ --input 输入文件位置 --out 输出文件位置 ./bin/flink run ./examples/batch/WordCount.jar ...
- es搭建过程会存在的问题 针对6.x
常见的四个基本错误 错误1 can not run elasticsearch as root 解决方案: 因为安全问题elasticsearch 不让用root用户直接运行,所以要创建新用户 第一步 ...
- [Tex学习笔记]让项目编号从4开始
微信扫描如上二维码关注跟锦数学微信公众账号. 详情请见那里.