-- -- Created by IntelliJ IDEA. -- User: koko -- Date: 15/5/21 -- Time: 下午8:48 -- To change this template use File | Settings | File Templates. -- -- lua的基本数据类型 -- lua的优势,1 可扩展性,可以很很多语言集成 2 简单,类似js -- 3 高效率(这个时我们采用他的主要原因) 4 与平台无关 -- lua的几种使用方式 --循环遍历 1 嵌入到应用程序中(我们也是应用在这个方面,和nginx结合,加速页面渲染) -- 2作为一种独立的语言 -- 3作为库供第三方调用 -- lua 官方网站 lua官网 http://www.lua.org -- 本章介绍 -- lua注释 -- 单行注释 -- [[ ]] 多行注释 -- 最简单的lua demo -- print("a") -- 保存为hello.lua -- 运行lua hello.lua即可 -- 全局变量和局部变量 --[[ local partVarible=1; globalVarible =1;]] -- loadfile 可以将代码加载 ,优点类似jsp里的include -- lua的几种基本数据类型 Nil,Boolean,Number,String,Function, Userdata and Threads -- 算数运算符 < > <= >= == ~=(和!=意思一样) -- 逻辑运算符 and or not -- 字符串链接运算符 .. -- 优先级 --[[ ^ not - (unary) */ +- .. < > <= >= ~= == and or ]] -- 表的构造 --[[days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}]] -- 赋值语句 --[[a = "hello" .. "world" t.n = t.n + 1 a, b = 10, 20]] -- 他里面的交换算法非常简单 --[[x, y = y, x -- swap 'x' for 'y' a[i], a[j] = a[j], a[i] ]] -- 控制语句 --[[if conditions then then-part end;]] --[[while condition do statements; end;]] --[[repeat statements; until conditions;]] --[[for var=exp1,exp2,exp3 do loop-part end]] --for i,v in ipairs(a) do print(v) end --for k in pairs(t) do print(k) end --[[ local a = {5,8,9,10}; for key,value in pairs(a) do print(key,value); end]] -- 函数多返回值 -- local a ,b = fun(); -- 闭包 ,grades 成为upvalue --[[ names = {"Peter", "Paul", "Mary"} grades = {Mary = 10, Paul = 7, Peter = 8} table.sort(names, function (n1, n2) return grades[n1] > grades[n2] -- compare the grades end ) for key,value in pairs(names) do print(key,value); end ]] --查看是否支持动态链接库 --print(loadlib()); --[[if pcall(openfile) then print("ok") end]] --[[ local status, err = pcall( function () error({code=121},2) end); print(err.code)]] --[[ co = coroutine.create(function () for i=1,10 do print("co", i) coroutine.yield() end end) coroutine.resume(co) --> co 1 print(coroutine.status(co))]] --[[ local authors = {} -- a set to collect authors function Entry (b) authors[b.author] = true end dofile("entry.lua") for name in pairs(authors) do print(name) end ]] -- 定义metatable --[[t={} t1 = {} setmetatable(t, t1) assert(getmetatable(t) == t1)]] -- metatable和metamethods 实例 --[[Set = {} function Set.new (t) local set = {} setmetatable(set, Set.mt) for _, l in ipairs(t) do set[l] = true end return set end function Set.union (a,b) local res = Set.new{} for k in pairs(a) do res[k] = true end for k in pairs(b) do res[k] = true end return res end function Set.intersection (a,b) local res = Set.new{} for k in pairs(a) do res[k] = b[k] end return res end function Set.tostring (set) local s = "{" local sep = "" for e in pairs(set) do s = s .. sep .. e sep = ", " end return s .. "}" end function Set.print (s) print(Set.tostring(s)) end Set.mt = {} -- metatable for sets s1 = Set.new({10, 20, 30, 50}) s2 = Set.new{30, 1} print(getmetatable(s1)) --> table: 00672B60 print(getmetatable(s2)) Set.mt.__add = Set.union s3 = s1 + s2 Set.print(s3) --> {1, 10, 20, 30, 50} Set.mt.__mul = Set.intersection Set.print((s1 + s2)*s1) --> {10, 20, 30, 50}]] -- __add ,__mul,__eq,__lt,__le __tostring(print打印的时候调用)这些metamethods都可以被重写 -- The __index Metamethod 特殊的metamethod --[[-- create a namespace Window = {} -- create the prototype with default values Window.prototype = {x=0, y=0, width=100, height=100 } -- create a metatable Window.mt = {} -- declare the constructor function function Window.new (o) setmetatable(o, Window.mt) return o end Window.mt.__index = function (table, key) return Window.prototype[key] end w = Window.new{x=10, y=20} w2 = Window.new{x=1111, y=2222 } print(w.x) --> 10 print(w2.x) --> 10 print(Window.prototype.x) --Window.mt.__index 没有必要是非得是函数,可以是一个表 -- 可以用如下形式 Window.mt.__index =Window.prototype;]] --当我们想不通过调用__index metamethod 来访问一个表,我们可以使用 rawget 函数。 Rawget(t,i)的调用以 raw access 方式访问表。 --这样查找的时候不会访问_index -- The __newindex Metamethod 用来对表进行访问 --[[ t = {} -- original table (created somewhere) -- keep a private access to original table local _t = t -- create proxy t = {} -- create metatable local mt = { __index = function (t,k) print("*access to element " .. tostring(k)) return _t[k] -- access the original table end, __newindex = function (t,k,v) print("*update of element " .. tostring(k) .. " to " .. tostring(v)) _t[k] = v -- update original table end } setmetatable(t, mt) t[2] = 'hello' print(t[2])]] -- 打印全局变量 --[[for n in pairs(_G) do print(n) end]] -- loadstring 5.3版以后已抛弃 --a = "123"; --loadstring("value =" .. a); -- 使用动态名字访问全局变量 -- string 库的 gfind 函数来迭代 f 中的所有单词(单词指一个或多个子母下划 线的序列) --[[ function getfield (f) local v = _G for w in string.gfind(f, "[%w_]+") do v = v[w] end return v end function setfield (f, v) local t = _G -- start with the table of globals for w, d in string.gfind(f, "([%w_]+)(.?)") do if d == "." then -- not last field? t[w] = t[w] or {} t = t[w] else t[w] = v -- create table if absent -- get the table -- last field -- do the assignment end end end setfield("t.x.y", 10) print(t.x.y) --> 10 print(getfield("t.x.y")) --> 10]] -- 非全局环境 --[[a = 1 -- create a global variable -- change current environment to a new empty table setfenv(1, {}) print(a) -- stdin:5: attempt to call global `print' (a nil value)]] --[[ a = 1 -- create a global variable -- change current environment setfenv(1, {_G = _G}) _G.print(a) --> nil _G.print(_G.a) --> 1]] -- (你必须在单独的 chunk 内运行这段代码,如果你在交互模式逐行运行他, -- 每一行 都是一个不同的函数,调用 setfenv 只会影响他自己的那一行。) -- 一旦你改变了你的环境, 所有全局访问都使用这个新的表,如果她为空,你就丢失所有你的全局变量, -- 甚至_G, 所以,你应该首先使用一些有用的值封装(populate)她,比如老的环境: --[[a=1 local newgt = {} -- create new environment setmetatable(newgt, {__index = _G}) setfenv(1, newgt) -- set it print(a) --> 1]] -- 当你访问"global" _G,他的值为旧的环境,其中你可以使用 print 函数。 你也可以使用继承封装(populate)你的新的环境: --[[ a=1 local newgt = {} -- create new environment setmetatable(newgt, {__index = _G}) setfenv(1, newgt) -- set it print(a) --> 1 a = 10 print(a) --> 10 print(_G.a) --> 1 _G.a = 20 print(_G.a) --> 20 ]]