lua数据结构
来源:互联网 发布:mac看不到隐藏文件夹 编辑:程序博客网 时间:2024/05/22 00:29
1.table
1.基础
1.说明
k-v数据结构,k非nil的任意类型
2.赋值
local tab = {} 初始化-- key 默认local tab = {"a", "b", "c"} 访问:tab[1] 。。。-- key 数字local tab = {[60]="a", [61]="b", [62]="c"}访问:tab[60] 。。。-- key 字符串local tab = {aa = "a", bb = "b", cc = "c"}访问:tab.aa 。。。
2.常用函数
1.方法一
local tab = {}
1.增
table.insert( tab, 元素)
2.删
table.remove( tab, 1 )
3.改
4.查
5.排序
local function sort(a, b) -- a>b倒序、a<b正序 return a < bendlocal tab = {1, 3, 100, 20, 90, 200, -100}table.sort(tab, sort)
2.方法二
1.长度:#tab
2.string
参考
1.基础
1.说明
1.索引:lua中字符串索引从前往后是1,2,……,从后往前是……,-2,-1
2.操作:string库中所有的function都不会修改原字符串,只返回一个结果
3.两种调用方式
local a = 'abcd' -- a操作、'abcd'索引--与 string.len(a) 效果相同, 其他string库函数也类似string.len(a) -- 传递self调用a:len() -- ":"代表a,面向对象调用
2.赋值
-- 1.一般赋值local str = 'adsd\n'local str = "aaa\04932"local str = [[adsds]]local str = [==[fsdfsdf]==]-- 2.转义字符print('\049') aprint('\n') 换行
2.常用函数
local s = "abcdef"
1.长度string.len(s) 6#s 62.重复string.rep(s,2) "abcdefabcdef" 重复两次3.子字符串-- string.sub(str, 开始下标, 结束下标(默认最后))string.sub(s,2) "bcdefg" 从2下标开始到最后string.sub(s,-2) "fg" 取后 2 个string.sub(s,2,-2) "bcdef" 从2下标开始到倒数第二个(包括第二个)string.sub(s,2,4) "bcd" 从2下标开始到下标44.大小string.lower("ABC") 返回 "abc"string.upper("abc") 返回 "ABC"5.转成字符串string.format("hh %s",s) 连接后 "hh abcdef"
3.正则
local s = "hello world"1.查找匹配位置string.find(s, "hello") 1,52.替换string.gsub(s, "o", "a") "hella, warld"
4.例子
1.分割字符串
local str = 'abcdef'for i=1,#str do local d = string.byte(str, i) local char = string.format("%c", d) print(char)end
5.编码格式
1.说明
1.汉字:一个汉字等于三个字符(字符范围 0~255)
2.特殊字符 :一个特殊字符(è)等于两个字符(字符范围 128~255)
3.其他字符: 0~127 ASSCII
举例
local str = '回好é1'local newstr,len = string.gsub(str,"[\128-\255]",'*')print(#newstr) 9=3+3+2+1print(len) 8=3+3+1+1 '回好**'
2.编码
unicode、ASCII、utf8、GBK、GB231
一般都能找到对应的转换库
1.转utf8
lua 转换到 utf8 编码格式,处理汉字和特殊字符的长度及求子字符串
https://github.com/tst2005/lua-utf8
3.数学math
1.绝对值 math.abs(-15) 结果:152.向下取整 math.floor(5.6) 结果:53.余数 math.fmod(-14,5) 结果:-44.分割整数小数 math.modf(5.6) 结果:5,0.65.最大 math.max(2.71, 100, -98, 23) 结果:1006.最小 math.min(2.71, 100, -98, 23) 结果:-987.随机 math.random(1, 100) 结果:1~1008.随机种子 math.randomseed(os.time())
阅读全文
0 0
- Lua数据结构
- lua数据结构
- Lua数据结构
- lua 数据结构
- Lua 数据结构
- Lua数据结构。
- lua数据结构
- lua的数据结构
- Lua的数据结构
- lua的数据结构 -- 数组
- lua的数据结构 -- 链表
- lua的数据结构 -- 队列
- lua中Table数据结构
- Lua 常用数据结构
- Lua数据结构 — T
- 细说Lua常用数据结构
- Lua学习之数据结构
- Lua学习笔记-数据结构
- 让心里住进阳光
- [Lintcode] #101 删除排序数组中的重复数字 II
- 关于并发
- [杂题] Codeforces #121C. Lucky Permutation
- Guitar Pro 7中的谱子缩放设置介绍
- lua数据结构
- cassandra插入二进制大文件超时问题
- 疑问
- Python多维数组初始化的两种方式和浅拷贝问题
- 抓包工具-Fiddler
- Java初体验之springMvc(二) helloworld
- docker的疑问
- JAVA知识_14
- aop的使用