-- lua 笔记--[[ 一、{} 遍历 ]]arr = { 1, 2, 3, 4 }-- 1.pairs 遍历出来顺序是随机的,且可能每次运行出来不一致。多用于作为字典时使用。-- pairs可以遍历表中所有的key,并且除了迭代器本身以及遍历表本身还可以返回nil;for _, v in pairs(arr) doio.write(v .. ' - ')endio.write('\n')-- 2.ipairs 作为数组时使用。保证其顺序。-- ipairs则不能返回nil,只能返回数字0,如果遇到nil则退出。它只能遍历到表中出现的第一个不是整数的keyfor _, v in ipairs(arr) doio.write(v .. ' - ')endio.write('\n')-- 3.for 作为数组时使用。保证其顺序。for i = 1, #arr doio.write(arr[i] .. ' - ')endio.write('\n')--[[ 二、安全访问 ]]test = a and a.b and a.b.c and a.b.c.d-- C# test = a.?b.?c.?d-- lua 中使用test = (((a or {}).b or {}).c or {}).d--[[ 三、 ]]-- C语言中的三目运算符 a ? b : c-- lua中使用--[[a and b or c --b不为false时等价if not a then a = b end 等价于 a = a or b -- lua中惯用写法.a为初始化时,用b进行初始化]]--[[ 四、lua的8中基本类型 ]]--[[nil, boolean, number(float, int ==> math.type()区分), string, userdata, function,thread, table ==>用type区分--]]--[[ 五、可变参数相关函数 ]]--[[ table.pack函数是获取一个索引从 1 开始的参数表 table,并会对这个 table 预定义一个字段 n,表示该表的长度。--]]-- 与之相对的函数:table.unpack()。参数为数组,返回值为数组内的所有元素。-- 例子:function add(...)local sum = 0local arg = table.pack(...)for i = 1, arg.n dosum = sum + arg[i]endreturn sumend-- select--[[select(n, ...) --数字n表示起点,select(n, ...)返回从起点n到结束的可变参数select('#', ...) --返回可变参数的数量--]]function add(...)local sum = 0for i = 1, select('#', ...) dosum = sum + select(i, ...)endreturn sumend--[[ 六、I/O ]]-- io.read(0) 用于测试是否达到文件末尾。如果仍然有数据可读,返回空字符;否则,返回nil。--[[ 七、瞬表 ]]-- 定义:一个具有弱引用键和强引用值的表是一个瞬表。-- 引用值中又引用了键的情况下,不计入键仍被引用。(避免了循环引用无法进行垃圾收集的尴尬场景)--[[ 七、协程 ]]--[[线程与协程的主要区别:1.一个多线程程序可以并行运行多个线程2.而协程需要彼此协作地运行,即在任意指定的时刻只能有一个协程运行,且只有当正在运行的协程显式地被要求挂起时其执行才会暂停。协程的四种状态:(可以通过函数coroutine.status(thread)查看)1.挂起2.运行3.正常4.死亡]]-- show codelocal co = coroutine.create(function() print('hello, thread') end)print(type(co)) --threadprint(coroutine.status(co)) --挂起(新创建的协程处于挂起状态不会自动运行)coroutine.resume(co) --启动或再次启动一个协程的执行print(coroutine.status(co)) --死亡-- yield函数挂起自己,然后用resume恢复执行co = coroutine.create(function()for i = 1, 10 doprint('co', i)coroutine.yield()endend)coroutine.resume(co)print('===回主线执行===')print(coroutine.status(co)) --挂起for i = 1, 10 docoroutine.resume(co)endprint(coroutine.resume(co)) --false cannot resume dead coroutine--[[当协程A唤醒协程B时,协程A既不是挂起状态(因为不能唤醒协程A),也不是运行状态(因为正在运行的是协程B)。所以,协程A此时的状态就被称为正常状态。]]-- 生产者和消费者function receive(prod)local status, val = coroutine.resume(prod)if not status thenerror(val)endreturn valendfunction send(x)coroutine.yield(x)endfunction producer()return coroutine.create(function()while true dolocal x = io.read()send(x)endend)endfunction filter(prod)return coroutine.create(function()for line = 1, math.huge dolocal x = receive(prod)x = string.format( "%5d %s", line, x)send(x)endend)endfunction consumer(prod)while true dolocal x = receive(prod)io.write(x, '\n')endend-- consumer(filter(producer()))-- 生成所有排列的函数(协程版本)function permgen(a, n)n = n or #aif n <= 1 thencoroutine.yield(a)elsefor i = 1, n doa[i], a[n] = a[n], a[i]permgen(a, n - 1)a[i], a[n] = a[n], a[i]endendendfunction printRes(a)for i = 1, #a doio.write(a[i], ' ')endio.write('\n')endfor p in coroutine.wrap(function() permgen({1, 2, 3}) end) doprintRes(p)endfor k, v in pairs(debug.getinfo(permgen)) doprint(string.format( "%s: %s", k, v))end--[[ 八、自省机制 ]]function getvarvalue(name, level, isenv)level = (level or 1) + 1 -- 因为在getvarvalue中调用,所以level + 1-- 最先从局部变量中查找for i = 1, math.huge dolocal n, v = debug.getlocal(level, i)if not n then break endif n == name then return 'local', v endend-- 尝试查找upvaluelocal func = debug.getinfo(level, 'f').funcfor i = 1, math.huge dolocal n, v = debug.getupvalue(func, i)if not n then break endif n == name then return 'upvalue', v endendif isenv then return 'noenv' end-- 从环境中查找local _, env = getvarvalue('_ENV', level, true) -- level 之所以用这个是因为又嵌套了一层函数(getvarvalue)if env thenreturn 'global', env[name]elsereturn 'noenv'endend-- _ENV = _G_ENV.aaa = 'test _ENV'local a = 42; print(getvarvalue('a'))print(string.format('_ENV.ttt = %s', _ENV.ttt))ttt = 'xxxxxx'; print(getvarvalue('ttt'))print(string.format('_ENV.ttt = %s', _ENV.ttt))print(string.format('_G.ttt = %s', _G.ttt))print(getvarvalue('aaa'))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。