lua语法学习2

table.concat()  -- 字符串拼接
table.maxn()
'%a+' 表示非空的字母序列;'%s*' 表示0个或多个空白
table.ceil(3.1)
math.randomseed(os.time())
math.random(5, 10)
math.modf(20.12) 20 0.12
math.mod() 取模
table.sort() --表排列
table.foreachi()  只输出数字的KEY
unpack (list [, i [, j]])  
功能:返回指定表的索引的值,i为起始索引,j为结束索引


t1 = {1, 2, 3, 5};
print(getn(t1))
table.setn(table, nSize)
next() 允许程序遍历表中的每一个字段,返回下一索引和该索引的值。
math.max()
math.min()
floor 向下取整 math.floor(9.9) 9
max 取参数最大值 math.max(2,4,6,8) 8
min 取参数最小值 math.min(2,4,6,8) 2
string.find()  返回 索引
string.sub()   返回字符串
s = "hello world"
local i, j = string.find(s, "hello")    --> 1    5
string.sub(s, i, j)        --> hello  截取


string.find(字符串查找)  --可以用f来表示 若是f开头表示是float那么就是数字  若是sub表示是string类型的参数
string.gsub(全局字符串替换)
string.gfind(全局字符串查找)
string.gmatch(返回查找到字符串的迭代器)




s = "hello world from Lua"
for w in string.gmatch(s, "%a+") do
 print(w)
end


local stringse = '"att":122,"def":111'
for arrti, value in string.gmatch(stringse,"%p(%a+)%p:(%d+)") do --"att":444
end


单个字符(除^$()%.[]*+-?外): 与该字符自身配对
%d: 与任何数字配对  单词digit的首字母
%a: 与任何字母配对  单词alphabetical的首字母
.(点): 与任何字符配对
%p: 与任何标点(punctuation)配对
%u: 与任何大写字母配对
%l: 与任何小写字母配对
%x: 与任何十六进制数配对


%s: 与空白字符配对
%c: 与任何控制符配对(例如\n)
%w: 与任何字母/数字配对
%z: 与任何代表0的字符配对


rawset (table, index, value)
功能:设置表中指定索引的值,此函数不会调用任何元表的方法,此函数将返回table


例如 rawset({"du", "gao", "da"}}, 1, "ddddd")  --table变为 dddddgaoda




string.upper() 大写表示
string.lower() 小写表示


local temp = {"du", "gao", "da"}
table.concat(temp, "..")  -- du..gao..da


table.foreach()
table.foreachi()


assert(v[,mesage]) -- 断言


pacall()




_VERSION 


collectgarbage("collect"): 执行垃圾回收的一个完整周期。 
collectgarbage("count"): 返回当前使用的千字节的程序内存量 


# 返回一个字符串或表的长度


table.concat(table, sep,  start, end)
concat是concatenate(连锁, 连接)的缩写. table.concat()函数列出参数中指定table的数组部分从start位置到end位置的所有元素,
 元素间以指定的分隔符(sep)隔开。除了table外, 其他的参数都不是必须的, 分隔符的默认值是空字符, start的默认值是1, end的默认值是数组部分的总长.




strbyte(string[, index]) - 转换字符串为整数值(可以指定某个字符).
strchar(asciiCode[, ...]) - 转换整数为相对应的字符
strsplit(delimiter, string) - 分割字符串


--------- 关于LUA源代码分析学习教程 ---------
lbaselib.c - 基础函数库  
lstrlib.c - 字符串库  
ltable.c - 表操作库  
lmathlib.c - 数学库  
loslib.c - 操作系统相关库  
liolib.c - 输入输入库  
loadlib.c - 模块库(实现require函数,package函数)  
ldblib.c - 调试库  
lapi.c - Lua的API.实现Lua C API(lua_*函数)集合  




---------- 数学库函数
sin 正弦 math.sin(math.rad(30)) 0.5
cos 余弦 math.cos(math.rad(60)) 0.5
tan 正切 math.tan(math.rad(45)) 1


module 的处理流程
require的处理流程
package.path:保存加载外部模块




> tbl = {"alpha", "beta", "gamma"}
> print(table.concat(tbl, ":"))  --alpha:beta:gamma


local result = string.split("1,2,3", ",")
-- result = {"1", "2", "3"}


-- 重载加法
local m = {
   __add = function(t1, t2)
     local sum = {}
     for key, value in pairs(t1) do
       sum[key] = value
     end
  
     for key, value in pairs(t2) do
       if sum[key] then
         sum[key] = sum[key] + value
       else
         sum[key] = value
       end
     end
     return sum
   end
 }
  
--将table1和table2都设置为m
 local table1 = setmetatable({10, 11, 12}, m)
 local table2 = setmetatable({13, 14, 15}, m)






setmetatable({1,1,1},{1,2,3})
local a = {}
a.__index = function(a,b)


end


a.__add = functinon(a,b)








end








 --函数os.clock()返回CPU时间的描述
 print(os.clock)




 io.output()
 io.input()
 io.write()




os库函数


    printInfo("dugaoda")
    local t1 = os.clock()
    print(t1)  -- 毫秒
    print(os.clock())
    print(os.clock())
    print(os.clock())
    print(os.clock())
    local t2 = os.time()
    print(t2)   -- 秒
--    os.time
    print(os.difftime(os.time(), t2))
os.difftime (t2, t1)
功能:返回t1到t2相差的秒数
print(os.time())
-->1249887340
print(os.time({year=1991,month=10,day=08}))
-->10500


--os.getenv(varname)
os.data()


os.date ([format [, time]])


功能:返回一个按format格式化日期、时间的字串或表
 若设置time参数,则按time指定的时间格式化,否则按当前时间格式化
















    getmetatable("abc") --
    getmetatable({1,2,3}) --nil




    local mt = {1,1,1,1}
    mt.__index = function(table, key)--参数table是t, key是a
        dump(table)
        print('table--'..tostring(table))
        print('key--'..key)
    end


    local t = {6,7,8,9}
    setmetatable(t, mt)
    local v = t.a


--    local mt = {}
--    mt.__index = {
--        a =10
--    }
--    local mt1 = {}
--    setmetatable(mt1, mt)
--    print(mt1.a)--输出10
















dump
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值