自己实验lua



--local bit32.band = bit32.band


parent = {  
  house = 1  
}  
  
parent.__index = parent    --如果没有这一句话  child即使是设置parent为元表  也不能找到parent中的内容. __index指向的内容是nil  
  
child = {  
  wife = 2  
}  
  
setmetatable(child, parent)  
print(child.house)  
print(child.wife)




parent.__newindex = function ( t, k ,v )  
    -- t[k] = v  
    if k == "house" then  
        parent[k] = v * 2  
    end  
end  
-- 等效于  
 --parent.__newindex = parent   
  
child.house = 3  
child.wife = 4  
  
print(child.house)  
print(child.wife)  
  
parent.__index = nil   
  
  
child.house = 5  
child.wife = 6  
  
print(child.house)  
print(child.wife) 


print("----------11111111111111111111111-")


function readOnly(t)
local proxy = {}
local mt = { -- create metatable
__index = t,
__newindex = function (t,k,v)
-- t[k] = v
    --rawset(t,k,v)
error("attempt to update a read-only table", 2)  --只读模式
end
}
setmetatable(proxy, mt)
return proxy
end


days = readOnly{"Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday"}
print (days[1])
--days[100]="x"  --attempt to update a read-only table 
--print (days[100])
print("----------11111111111111111111111-")


local and1 = 0
local and2 = 1


--local andRe = bit32.band(1,2)
local andRe = and1 and and1 or and2
print ("andRe="..andRe)
local andRe2 = and1 or and2
print ("andRe2="..andRe2)
local andRe3 = 0
if (and1 == 0 and 1 == 1 ) or (and2 == 1 and 2 == 3)then
andRe3 = 1
else  
    andRe3 = 0
end
print ("andRe3="..andRe3)


function aa()
print("function aa")
return 1
end
function bb(para)
print ("function bb")
end


local x = bb(aa())


print ("------------------222222222222---------------")
local a={}
local b={2,1,3}
a=b
b={4,5}
--b[1]=4
table.sort(a)
for k,v in pairs(a) do
print (v)
end


print ("------------------33333333---------------")
local t3 = {[1]='11',[2]=22,[3]=33,mm="mm"}
t3.x = 2
if t3.x == nil or t3.x == 2 then
--if t3.ttt == 1 then
print ("t3.x == nil")
end


table.remove(t3,mm)
for k,v in pairs(t3) do
print (v)
end


print ("------------------4444444---------------")
print (os.time())
print (os.date("*t",os.time()))
print (tonumber(os.date("*t",os.time())))
print ("------------------555555---------------")
function GetT5(type)
if type  == 1 then
return nil
elseif type == 3 then
   return nil
end
return type
end
local t5 = {1,2,3,4,5}
local t5temp = {6,7}
table.insert(t5,t5temp)
for k,v in pairs(t5) do
local temp = GetT5(k)
if temp ~= nil and type(temp) ~= "table" then
   print ("temp="..temp)
end
end
print ("------------------666666 table能作为函数参数?---------------")
function TestT6(tab)
if type(tab) == "table" then 
print("type(tab) == table,tab[2]="..tab[2])
end
end
TestT6(t5)
print ("------------------777777协同程序---------------")
co = coroutine.create(function ()
print("hi")
end)
print(co)
print(coroutine.status(co)) --> suspended
coroutine.resume(co) --> hi
print(coroutine.status(co)) --> dead


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)) --> suspended
coroutine.resume(co) --> co 2
coroutine.resume(co)
coroutine.resume(co)
coroutine.resume(co)
coroutine.resume(co)
coroutine.resume(co)
coroutine.resume(co)
coroutine.resume(co)
coroutine.resume(co)


print ("------------------8888888协同程序 好像调用的地方有问题---------------")


function receive (prod)
local status, value = coroutine.resume(prod)
return value
end


function send (x)
coroutine.yield(x)
end


function producer ()
    return coroutine.create(function ()
    while true do
local x = io.read() -- produce new value
send(x)
    end
    end)
end


function filter (prod)
return coroutine.create(function ()
local line = 1
while true do
local x = receive(prod) -- get new value
x = string.format("%5d %s", line, x)
send(x) -- send it to consumer
line = line + 1
end
end)
end


function consumer (prod)
while true do
local x = receive(prod) -- get new value
--io.write(x, "\n") -- consume new value
io.write(x, " ") -- consume new value
end
end


io.input("F:\\MyTest\\LuaTest\\luatest1\\test.txt")
-- 调用1
p = producer()
f = filter(p)
--consumer(f)
--或者
-- 调用2
--consumer(filter(producer()))


print ("------------------99999999999--------------")
--f(n)=f(n-)+f(n-2)
fib = coroutine.create(
   function ()
      local x, y = 1, 1
      while true do
         coroutine.yield(x,y)
print("x=",x)
         x, y = y, x + y
      end
   end)


for i = 1, 10 do
local status,value_x,value_y = coroutine.resume(fib)
    print(value_x.." "..value_y)
end


print ("------------------111111111111--------------")
function foo(a)
    print("foo", a)
    return coroutine.yield(2 * a)
end


co = coroutine.create(function ( a, b )
    print("co-body", a, b)
    local r = foo(a + 1)
    print("co-body", r)
    local r, s = coroutine.yield(a + b, a - b)
    print("co-body", r, s)
    return b, "end"
end)


print("main", coroutine.resume(co, 1, 10))
print("main", coroutine.resume(co, "r"))
print("main", coroutine.resume(co, "x", "y"))
print("main", coroutine.resume(co, "x", "y"))
print ("------------------2222222222222--------------")
local t22 = {1,a="b",{100,m="n"}}
print (t22["a"])
for k,v in pairs(t22) do
print("k="..k)
end
print ("------------------333333333 string match gmatch--------------")
local regTest = "31230af"
local regR = string.match(regTest,"%d*")
if regR ~= nil then
print("if regR ~= nil then..regR="..regR)
end
local hanzi = "我是汉字abc1234呵呵"
for ch in string.gmatch(hanzi,"[%0-%x7F%xC2-%xF4][%x80-%xBF]*") do
print(ch,#ch~=1)
end
print ("------------------44444444444--------------")
function strTest(name,to)
local test = {}
test.name = name
test.to = test.to
print(test.name..to)
end
strTest("你好","2er打人")
print(math.random(1,10))
print ("------------------5555555555--------------")
local table5 = {[1]=2,x = "y"}


local temp5 = {}
table.insert(temp5,table5)


local table5_tmp = table5
table5[1] = 3
print(table5_tmp[1]..table5_tmp["x"])


print(temp5[1][1])
print ("------------------666666666666--------------")
for i = 1,30 do
--print(math.random(65534000,65545000))
print(math.random(3))
end
print ("------------------7777777777--------------")
local fjsdk = nil
if fjsdk == 1 then
print("==1")
end
print ("------------------888888--------------")
local date_1 = 20160329
local date_2 = 20160409
local table_1 = {year = tonumber(string.sub(date_1,1,4)),month = tonumber(string.sub(date_1,5,6)), day = tonumber(string.sub(date_1,7,8))}
local table_2 = {year = tonumber(string.sub(date_2,1,4)),month = tonumber(string.sub(date_2,5,6)), day = tonumber(string.sub(date_2,7,8))}
print(os.difftime(os.time(table_2),os.time(table_1))/(24*60*60) + 1)
local nowdate = tonumber(os.date("%Y%m%d",os.time()))
print(nowdate)
print(string.sub(date_1,1,4))
print(string.sub(date_1,5,6))
print(string.sub(date_1,7,8))
print(os.time(table_2))
print(os.time())


print(math.ceil(0.2))
print(math.ceil(2.0))


local  x1 = "^^^"..2 
local x2 = "fd"
local x3 = "4fjkd"
local x4 = x1..x2..x3
print(x4)
print ("------------------99999--------------")
local test9 = {1,2,x = "y"}
for k,v in pairs(test9) do
print (v)
end


if 1> 0 and 3 >= #test9 then
print("1 > #test9")
end
local x9 = "nihaoa"
test9[x9] = "hhh"
for k,v in pairs(test9) do
print(k.." v="..v)
end


if xxxxx == false then
print("xxxx==false")
end
print ("------------------0000000000--------------")


local t00 = 3
local t01 = {1,2,t00 = t00}
for k,v in pairs(t01) do
print (k.." "..v)
end


print (math.ceil(2.2))
print ("------------------1111111111--------------")


print(math.ceil(190/180))


print(os.date("%y%m%d%H",os.time()))
print ("------------------2222222222--------------")
local  ttt2 = {
  --x222 =1,y222=2,z222=self.x222,
}
for k,v in pairs(ttt2) do
print (k.." "..v)
end
if type({}) == "table" then
print("fsdfsdfsdfd")
end


print (math.min(2,3))
print ("------------------3333333333--------------")
local tt33 = {2,3}
local temp33 = {4}
table.insert(temp33,tt33)
tt33[2] = 10
for k,v in pairs(temp33) do
print (k)
if k == 2 then
v[2] = 11
for i,j in pairs(v) do
print("v="..j)
end
end
end
for k,v in pairs(temp33) do
print (k)
if k == 2 then
for i,j in pairs(v) do
print("v="..j)
end
end
end
print(math.random(10,20)/10)
print ("------------------4444444444--------------")
local tt44 = {[123]=1,[3123]=2}
for k,v in pairs(tt44) do
--v = v + 1
tt44[k] = tt44[k] + 1
end
table.remove(tt44,1)
for k,v in pairs(tt44) do
print(v)--v = v + 1
end
print ("------------------555555555555--------------")
print(#{})


print ("hehe"..tostring(true))
print(table.getn({[212]=1,['z']=x,y,{z}}))
local bool5  = true
if bool5 then
print("bool5")
end
print ("------------------66666666--------------")
local ttt666 = {3,50,17,9}
for k,v in pairs(ttt666) do
print(k.." "..v)
end
print("sort")
table.sort(ttt666)
for k,v in pairs(ttt666) do
print(k.." "..v)
end
print(os.date("*t",os.time()).sec)
local table66 = os.date("%Y%m%d%H",1461896934)
print(table66)
if 3 > 1 and  not (1 > 2) then
print("2>1")
end
local iii,jjj,kkk = -1,-1,-1
print(jjj)
print(os.date("%Y%m%d%H%m%s",1461999350))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值