- Lua中ipairs遇到nil要断
- 在Lua中,都是查找__index,跟元表本身的字段没有关系。
- 在Lua中,__newindex,跟自己的元表有没有字段也没有关系,跟自己有关系。
- 这些函数里面,一个是t,一个k,请注意不是值哦
- setmatatable中,第一个是自己,第二个是元表。
- lua特有运算,
- +0.0转换为整型
- |0转换为浮点
- //向下取整
- ^0.5幂运算
- Lua中无论变量在哪里没加local,都是全局变量
- Lua中错误大部分是标点等问题,而且依赖关系是可以叠加的,不用全部都要引用呢!!!
- Lua种模块名和文件必须一致呢
- Lua两次Require都是一样的东西,如果没有返回值,值只是代表是否成功而已呢!
- require 加载一次,dofile(x.lua)可以重复执行,不过每个lua代码都是一个Chunk,相当于一个函数,其中有代码是会执行的呢。
- 在Lua中,变量和函数要先声明再使用,
- Lua表中可以放置变量,并且是字符串的key
- Lua中ipairs要断,pairs不打印nil,但是长度一直是有的,但是尾部的nil会忽略
含有nil,就不要用#,用table.maxn() - Lua中0做字典索引要小心,做字典和数组连着用会有奇怪的问题!!小心
local g ={[0]=“monde”,“skd”,“biler”};这种不算是字典吧,其实是算的#会考虑
Lua k,v交换有点诡异,for循环哪里 不打标点有空格不会报错! - lua,()除了提高运算等级,还可以只强制返回一个返回值
#只看长度截断的地方,有负数索引也不对 - table中key是无序的,但是luajit对它做了处理,只编译一次
- 技巧:可以用and判断是否是nil,or 0作为初始值
- Lua中,table[1]表示第一个混合不是kv的值,请注意哦
- 练习部分代码:
-- luajit5.1 study
-- print(2 ^ 3)
-- print("我是中国人!");
-- function a()
-- return function()
-- print("kiki")
-- print(bi)
-- end
-- end
-- a()()
-- function J()
-- b, bi = 3, 2
-- end
-- J()
-- print(b, bi)
-- a = print
-- a("nihaoma")
-- a(2e+3)
-- b,c ="hello","world";
-- d =string.gsub(b,"ll","");
-- print(d);
-- print("ssss\fs");
-- print("s\fs");
-- str =[===[ a=b[c[i]] wo ai akk \b]===];
-- print(str);
-- print("10"+33);
-- local ar =1 ..1;
-- print(type(ar));
-- print("hello" .. "world")
-- print(type(tonumber("3332")));
-- jj={a=32};
-- print(type(jj.a));
-- --lua叫模块,c叫包
-- print(loadlib);
-- c ={name="siki"};
-- b =c;
-- k =c;
-- c =nil;
-- print(k.name);
-- b={name="siki"};
-- print(b==k);
-- local c = {1, 2, nil, 3, nil, 3, nil}
-- --print(c["x"]);
-- --print(#c);
-- --print(c[1]);
-- for index, value in ipairs(c) do
-- -- body
-- --print(index,value);
-- end
-- --ipairs要断,pairs不打印nil,但是长度一直是有的,但是尾部的nil会忽略
-- -- print(#c);
-- -- print(10 ..2);
-- a = {"a", 2, "siki", "b", "c", "d", false}
-- a[1] = "w"
-- a[0] = "chiji"
-- a[2] = "woaini"
-- a["3"] = "hah"
-- a[8] = function()
-- print("")
-- end
-- --a[1]="nibaba";
-- -- a["j"] =54;
-- -- a["bike"]=88;
-- --a[2] ="woki";
-- -- a[4] =1;
-- -- print(#a);
-- -- for index, value in pairs(a) do
-- -- print(index,value);
-- -- end
-- --print(table.maxn(a));
-- -- c,d =0,"0";
-- -- a[c] ="2";
-- -- a[d] =32;
-- -- a[2] = 88
-- -- a["1"] = "77"
-- -- a[0] = 55
-- -- for key, value in ipairs(a) do
-- -- print(key, value) -- body
-- -- end
-- -- a[1]=1;
-- -- a[2]=true;
-- -- a[3]=3;
-- for index, value in ipairs(a) do
-- print(index, value)
-- -- body
-- end
-- --print(a[2])
-- print(a["2"])
-- print(#a)
-- local x = 1.245
-- print(x - x % 1)
-- x =math.pi;
-- print(x -x%0.01);
-- print(-180%-360);
-- print(nil == nil)
-- local k
-- k = nil or 2
-- print(k)
-- function Add(a, b)
-- a = a or 3
-- b = b or 4
-- return a, b
-- end
-- local g, c = Add()
-- print(g, c)
-- local k = false
-- local a, b = 2, 3
-- local j = k and a or b
-- local k =0;
-- local c,k =5,6;
-- local j2 =k and c or k;
-- print(j2);
-- local k = {"siki", "jj"}
-- print(#k)
-- k[#k] = nil
-- print(#k)
-- local g ={"monde","skd","biler";2,1,1,};
-- print(g[0]);
-- print(g[1]);
-- print(g[2]);
-- print(#g);
-- for i = 10, 1, -1 do
-- print(i)
-- end
-- -- 使用循环来打印
-- local str3 = "helloworld from Lua 11"
-- local index = 1
-- --要空格
-- for word in string.gmatch(str3, "%w+") do -- 查找目标字符串中的任何字母相匹配
-- print(word)
-- --index = index + 1
-- end
-- function JJ(...)
-- local k ={...};
-- return 2,3,5;
-- end;
-- local k = {[1] = "a", [2] = "b", [3] = "c"}
-- -- --这个其实是key
-- for k1, v in pairs(k) do
-- k[v] = k1
-- print("chiji");
-- end
-- for k1, v in pairs(k) do
-- print(k1, v,"hh");
-- end
-- function foo()
-- -- body
-- do
-- return
-- end
-- print("jjk")
-- -- do return end;
-- end
-- function K(t)
-- print(#t)
-- -- body
-- end
-- local k2 = {1, 2, 3}
-- K {a = "aa", b = "ccc"}
-- function a()
-- return 2,3; -- body
-- end
-- x,y =a(),5;
-- print(x,y);
-- print(a().."siki");
-- for index, value in ipairs({a()}) do
-- print(index,value); -- body
-- end
-- print((a()));
-- --foo();
-- function b()
-- return (a());
-- end;
-- x,y =b();
-- print(x,y);
-- local k ={1,2,3};
-- local k2 =print;
-- k2(unpack(k));
--...是多返回值的函数而已
-- function k(...)
-- return ...
-- end
-- local a, b, c = k(1, 2, 3)
-- print(a, b, c)
-- function j(a, ...)
-- print(a .. "woaini")
-- end
--j(2,33,4);
--select不过是个函数,"#"返回总数,i返回单个,目的是为了对抗...中的nil,因为ipairs nil会断,pairs nil会忽略
-- function JJk(b,...)
-- local jj = {...}
-- print(#jj)
-- -- for index, value in pairs(jj) do
-- -- print(index,value);
-- -- end
-- for i = 1, select("#", ...) do
-- local arg = select(i, ...)
-- print(arg)
-- end
-- end
-- JJk(1,2, 3, nil, 5)
-- function woain(a)
-- a.name = a.name or "bill"
-- a.age = a.age or 32
-- a.work = a.work or "codeing"
-- print(a.name, a.age, a.work)
-- end
-- woain {age = 32, name = "gale"}
-- bill =print;
-- print =nil;
-- --print("woani");
-- bill("woaini");
-- wo = {
-- {name = "bill", age = 323},
-- {name = "gate", age = 33},
-- {name = "ss", age = 555}
-- }
-- table.sort(
-- wo,
-- function(a, b)
-- return a.age < b.age
-- end
-- )
-- for key, value in pairs(wo) do
-- print(value.name, value.age) -- body
-- end
--loadstring比dofile编译少
-- i =3;
-- f =loadstring("i =i+1");
-- f();
-- print(i);
-- i =32
-- local i =0;
-- f =loadstring("i=i+1;print(i)");
-- g =function() i =i+1; print(i) end;
-- f();
-- g();
-- local l = " x,y,z";
-- local f = assert(loadstring("local x,y,z =...;return"..l))
-- local a,b,c=f(1,2,3);
-- print(a,b,c);
-- local k ="false";
-- assert(tonumber(k),"input:"..k.."not number");
-- local file, msg
-- repeat
-- print "enter a file name:"
-- local name = io.read()
-- if not name then
-- return
-- end
-- file, msg = io.open(name, "r")
-- if not file then
-- print(msg)
-- end
-- until file
-- function foo()
-- local a = nil
-- print(a[1])
-- end
-- if pcall(function() local a =nil;print(a[1]) end) then
-- else
-- print("erro")
-- end
-- local status,err = pcall(function() error({code=123})end);
-- print(err.code);
-- print("woaini")
--xpcall比pcall在返回前就获取错误真正的地方呢,而且和debug连着用呢
-- function foo(str)
-- if(type(str)~="string") then
-- error("string expected",2);
-- end;
-- end;
-- foo({x=1});
--print(debug.debug("nimaya"));
--print(debug.traceback());
-- local k={}
-- for i=5,10 do
-- k[i] =i;
-- end;
-- print(k[1]);
-- print(#k);
-- mt ={}
-- for i=1,N do
-- mt[i] ={};
-- for j=1,M do
-- mt[i][j] =0;
-- end;
-- end;
-- local k ={a="aa",b="bb",c="cc",d="dd";e="f",};
-- for index, value in pairs(k) do
-- -- body
-- print(index,value);
-- end
local k = {2, 3, 4, 5}
-- function caozuo(k)
-- local list = nil
-- for index, value in ipairs(k) do
-- -- body
-- list = {next = list, value = value}
-- end
-- return list
-- end
-- local l = caozuo(k);
-- while l do
-- print(l.value)
-- l = l.next
-- end
-- local k = {}
-- function enqueue(val)
-- table.insert(k, val)
-- end
-- function dequeue()
-- table.remove(k,1);
-- end
-- function printAll()
-- for index, value in ipairs(k) do
-- print(value)
-- -- body
-- end
-- end
-- enqueue(5)
-- enqueue(3)
-- enqueue(2)
-- enqueue(4)
-- dequeue()
-- dequeue()
-- printAll()
-- local k ={c=true,b=false,c=true};
-- for key, value in pairs(k) do
-- if not value then
-- print("woaini");
-- end;
-- end
-- local t ={"wo","men","ai","ni","ya","ha","ha"};
-- t[#t+1]="";
-- local s = table.concat(t,"狗日");
-- print(s);
-- local function name2node(graph, name)
-- -- body
-- if not graph[name] then
-- graph[name] = {name = name, adj = {}}
-- end
-- return graph[name]
-- end
-- function readgraph()
-- local graph = {}
-- for line in io.lines() do
-- local namefrom, nameto = string.match(line, "(%S+)%s+(%S+)")
-- local from = name2node(graph, namefrom)
-- local to = name2node(graph, nameto)
-- from.adj[to] = true
-- end
-- return graph
-- end
-- function findpath(curr,to,path,visited)
-- path = path or {}
-- visited = visited or {}
-- if visited[curr] then return nil end;
-- visited[curr] =true;
-- path[#path+1] =curr;
-- if curr ==to then return path end;
-- for node in pairs(curr.adj) do
-- local p =findpath(node,to,path,visited)
-- if p then return p end;
-- end
-- path[#path] =nil;
-- end;
-- local str =[[
-- Entry {
-- auther ="gate",
-- year =1992,
-- title ="bill"
-- }
-- Entry {
-- 1,2,3
-- }
-- ]]
-- function Entry(t)
-- for key, value in pairs(t) do
-- print(key,value);
-- end
-- end;
-- local g=loadstring(str);
-- g();
-- a ='a"problematic"\\string'
-- print(a);
-- print(string.format("%q",a));--接受一个字符串并将其转化为可安全被Lua编译器读入的格式
--string.format(" [%s[\n%s]%s",eq,s,eq)
-- function serialize(o)
-- if type(o)=="number" then
-- io.write(o);
-- else if type(o)=="string" then
-- io.write(string.format("%q",o))
-- end;
-- end;
-- end;
--带环的table和共享子table
-- a={x=1,y=2;{3,4,5}};
-- a[2] =a;
-- a.z =a[1];
-- for index, value in pairs(a) do
-- print(index,value);
-- end
-- t1 ={};
-- assert(true);
-- 元方法只需要一个就可以了呢。
--__eq __lt __le
--小于等于和小于都要提供实现!
--不像算术的混合使用,而且两个不同元方法也只是false
--共享一个元表嘛
-- function Set.new(l)
-- local set = {}
-- setmetatable(set, mt)
-- for _, v in ipairs(l) do
-- set[v] = true
-- end
-- return set
-- end
-- local mt = {}
-- --mt.__tostring=function() print("hhh") end;
-- --保护元表
-- local k = {}
-- --mt.__metatable = "not your business"
-- mt.__tostring="woaini";
-- setmetatable(k, mt)
-- local jj = getmetatable(k)
-- -- setmetatable(k, {name = "siki"})
-- print(jj)
-- --__tostring可能被tolua改写了
-- mytable={"lua","java","c#"}
-- mymetatable={
-- __tostring=function(mytable)
-- local str=""
-- for k,v in pairs(mytable) do
-- str=str..v..","
-- end
-- return str
-- end
-- }
-- mytable=setmetatable(mytable,mymetatable)
-- print(mytable)
-- index newindex 查询和修改代理table中不存在的字段,两个都可以是table或者函数
--具有默认值的table
-- k ={name ="siki"};
-- b ={};
-- k.__index =k;
-- setmetatable(b,k);
-- k =nil;
-- print(b.name);
-- function setDefault(t,d)
-- local mt ={__index =function() return d end;}
-- setmetatable(t,mt);
-- end;
-- tab ={x=10,y =10};
-- print(tab.x,tab.z);
-- setDefault(tab,0);
-- print(tab.x,tab.z);
--index函数的t不是元表哦
-- local mt ={__index =function(t) return t.___ end};
-- function setDefault(t,d)
-- t.___ =d;
-- setmetatable(t,mt);
-- end;
-- local a ={x=2,y =3};
-- print(a.x,a.z);
-- setDefault(a,99);
-- print(a.x,a.z);
--跟踪table的访问
-- t = {} --原来的table
-- local _t = t --保持对原table的一个私有访问
-- --创建代理
-- t = {}
-- local mt = {
-- __index = function(t, k)
-- print("*access to element" .. tostring(k))
-- return _t[k]
-- end,
-- __newindex =function(t,k,v)
-- print("*update of element"..tostring(k).."to"..tostring(v))
-- _t[k] =v;
-- end;
-- }
-- setmetatable(t,mt);
-- t[2] ="hello";
-- print(t[2]);
-- function track(t)
-- local proxy ={};
-- proxy[index] =t;
-- setmetatable(proxy,mt)
-- return proxy;
-- end;
--通过代理创建只读确实很秒啊
--raw
-- t={};
-- local _t =t;
-- t ={
-- __index =
-- };
-- key ={};
-- function track(t)
-- local daili ={};
-- local mt ={
-- __index =t,
-- __newindex =function(t,k,v)
-- error("no access");
-- end;
-- }
-- setmetatable(daili,mt);
-- return daili;
-- end;
-- local j =track{"a","b","c"};
-- --print(j[1]);
-- j[2] ="22";
-- function track(t)
-- local daili = {}
-- local mt = {
-- __index = t,
-- __newindex = function(t, k, v)
-- t[k] = v
-- end
-- }
-- setmetatable(daili, mt)
-- return daili
-- end
-- local j = track {"a", "b", "c"}
-- rawset(j,1,"aA");
-- rawset(j,2,"b");
-- rawset(j,3,"CC");
-- print(j[1]);
-- print(j[2]);
-- print(j[3]);
-- local bbbbKK = 3
-- local function Aaa333()
-- print("aa")
-- end
-- _G.Aaa333();
-- for n in pairs(_G) do
-- if (n == "Aaa333") or (n == "bbbbKK") then
-- print(n)
-- end
-- end
-- value =_G[x];
-- print(_G[x]);
-- function getfield(f)
-- local v=_G;
-- for w in string.gmatch(f,"[%w_]+")do
-- v=v[w]
-- end;
-- return v;
-- end;
-- function setfield(f,v)
-- local t =_G;
-- for w,d in string.gmatch(f,"([%w_]+(%.?)") do
-- end;
-- end;
-- local declareNames = {}
-- setmetatable(
-- _G,
-- {
-- __newindex = function(_, n, v)
-- if not declareNames[n] then
-- local w = debug.getinfo(2, "S").what
-- if w ~= "main" and w ~= "C" then
-- error(" change no var" .. n, 2)
-- end
-- declareNames[n] = true
-- end
-- rawset(_, n, v)
-- end,
-- __index = function(_, n)
-- if not declareNames[n] then
-- error(" access no var " .. n, 2)
-- else
-- return nil
-- end
-- end
-- }
-- )
-- print(g);
-- a =1;
-- setfenv(1,{g=_G});
-- g.print(a);
-- g.print(g.a);
-- a=1;
-- local newgt ={}
-- setmetatable(newgt,{__index=_G})
-- setfenv(1,newgt);
-- print(a);
-- a = 10
-- print(a)
-- print(_G.a)
-- _G.a = 20
-- print(_G.a)
-- function factory()
-- return function()
-- return a
-- end
-- end
-- a =3;
-- f1=factory();
-- f2 =factory();
-- print(f1());
-- print(f2());
-- setfenv(f1,{a=10})
-- print(f1());
-- print(f2());
-- package.loaded["foo"] =nil;
-- require "foo";
-- print(package.cpath);
-- print(package.path);
-- require "Test2"
-- aini.chihe();
-- A = {blance = 100}
-- function A:with(v)
-- self.blance = self.blance - v
-- end
-- function A:new()
-- local b = {}
-- self.__index = self
-- setmetatable(b, self)
-- return b
-- end
-- a = A:new();
-- b = A:new();
-- -- =nil;
-- A=nil;
-- a:with(20);
-- print(a.blance);
-- print(b.blance);
--print(a.blance);
--第一次调用deposit时,对表达式b.blance求值结果为0,不会涉及元方法了,因为此时b已有自己的balance字段了呢
-- Account = {balance = 0}
-- function Account:new(o)
-- o = o or {}
-- setmetatable(o, self)
-- self.__index = self
-- return o
-- end
-- function Account:deposit(v)
-- self.balance = self.balance + v
-- end
-- function Account:withdraw(v)
-- if v > self.balance then
-- error("insufficenent funs")
-- end
-- self.balance = self.balance - v
-- end
-- SpecialAccount = Account:new()
-- function SpecialAccount:withdraw(v)
-- if v - self.balance >= self:getLimit() then
-- error("insufficient funds")
-- end
-- self.balance = self.balance - v
-- end
-- function SpecialAccount:getLimit()
-- return self.limit or 0
-- end
-- s = SpecialAccount:new {limit = 1000}
-- function s:getLimit()
-- return self.balance*0.10
-- end;
-- --s:withdraw(200);
-- local function search(k, plist)
-- -- body
-- for i = 1, #plist do
-- local v = plist[i][k]
-- if v then
-- return v
-- end
-- end
-- end
-- function createClass(...)
-- local c = {}
-- local parents = {...}
-- setmetatable(
-- c,
-- {
-- __index = function(t, k)
-- return search(k, parents)
-- end
-- }
-- )
-- c.__index = c-->这里直接搜索不好吗???
-- function c:new(o)
-- o = o or {}
-- setmetatable(o, c)
-- return o
-- end
-- return c
-- end
-- Named ={}
-- function Named:getname()
-- return self.name;
-- end;
-- function Named:setname(n)
-- self.name =n;
-- end;
-- NamedAccount = createClass(Account,Named);--c
-- account =NamedAccount:new {name ="Paul"};--o
-- account:deposit(100);
-- account:setname("bill");
-- print(account:getname());
-- print(account.balance);
-- local j= require "Test2";
-- j.JJ();
-- function newObject(value)
-- return function(action, v)
-- if action == "get" then
-- return value
-- elseif action == "set" then
-- value = v
-- else
-- error("invalid action")
-- end
-- end
-- end
-- d = newObject(0);
-- print(d("get"));
-- d("set",10);
-- print(d("get"));
-- d2 = newObject(23);
-- print(d2("get"));
-- print(d("get"));
-- function pairsByKeys(t, f)
-- local a = {}
-- for n in pairs(t) do
-- a[#a + 1] = n
-- end
-- table.sort(a, f)
-- local i = 0
-- return function()
-- i = i + 1
-- return a[i], t[a[i]]
-- end
-- end
-- table.rconcat{"a","b","c"};
-- local a ="aAA";
-- local s ="[in brackets]"
-- print(string.sub(s,2,-2));
-- print(string.char(99,100,101));
-- print(string.byte("abc",1,2));
-- print(string.format("pi=%.5f",math.pi));
-- d =5;m=11;y =1990;
-- print(string.format("%02d/%02d/%04d",d,m,y));
--gsub全局匹配
--模式匹配的%和string.format的是不一样的哦
--os.remove();
--os.exit();
--print("woaniio");
-- print(os.getenv("HOME"))
-- ;
-- os.execute("mkdir aini");
-- os.setlocale();
-- function trace(event,line)
-- local s = debug.getinfo(2).short_src;
-- print(s..":"..line);
-- end;
-- debug.sethook(trace,"l");
-- function newCounter()
-- local i =0;
-- return function()
-- i =i+1;
-- return i;
-- end;
-- end;
-- c1 = newCounter();
-- print(c1());
-- print(c1());
-- c2 = newCounter();
-- print(c2());
-- print(c2());
-- print(c2());
-- print(c1());
-- function values(t)
-- local i = 0
-- return function()
-- i = i + 1
-- return t[i]
-- end
-- end
-- t = {10, 20, 30}
-- iter = values(t)
-- while true do
-- local el = iter()
-- if el == nil then
-- break
-- end
-- print(el)
-- end
-- for el in values(t) do
-- print(el);
-- end;
-- a ={}
-- b ={__mode ="k"};
-- setmetatable(a,b);
-- key ={}
-- a[key] =1;
-- key ={};
-- a[key]=2;
-- collectgarbage();
-- for k,v in pairs(a) do print(v) end;
-- local results = {}
-- setmetatable(results,{__mode="kv"});
-- function mem_loadstring(s)
-- local res = results[s]
-- if res == nil then
-- res = assert(loadstring(s))
-- results[s] = res
-- end
-- return res
-- end
-- local defaults ={}
-- setmetatable(defaults,{__mode="k"});
-- local mt ={__index =function(t) return defaults[t] end;}
-- function setDefault(t,d)
-- defaults[t] =d;
-- setmetatable(t,mt);
-- end;
-- local metas = {}
-- setmetatable(metas, {__mode = "v"})
-- function setDefault(t, d)
-- local mt = metas[d]
-- if mt == nil then
-- mt = {__index = function()
-- return d
-- end}
-- metas[d] = mt
-- end
-- setmetatable(t, mt)
-- end
-- local j ={};
-- setDefault(j,33);
-- print(j.name);
-- require "Third.functions";
-- B =class();
-- function B:ctor(x, y)
-- self.x = x
-- self.y = y
-- end
-- function B:addOne()
-- self.x = self.x + 1
-- self.y = self.y + 1
-- end
-- function B:printTest()
-- print(self.x, self.y)
-- end
-- C = class(B);
-- function C:ctor(x, y, z)
-- self.x = x;
-- self.y = y;
-- self.z = z;
-- end
-- function C:addTen()
-- self.x = self.x + 10;
-- self.y = self.y + 10;
-- self.z = self.z + 10;
-- end
-- function C:printTest()
-- -- C.superclass.printTest(self);
-- print(self.x, self.y, self.z);
-- end
-- -- b =B.new(3,3);
-- -- b:addOne();
-- -- b:printTest();
-- c =C.new(5,5,5);
-- c.addOne(c);
-- c:addTen();
-- c:printTest();