加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

lua 笔记

发布时间:2020-12-14 22:15:47 所属栏目:大数据 来源:网络整理
导读:lua基本类型:nil、boolean、string、userdata、function、thread、table函数type返回类型Lua 保留字:and break do else elseifend false for function if in local nil not or repeat return then true until while 运算符: = = == ~=逻辑运算符:and or
lua基本类型:
nil、boolean、string、userdata、function、thread、table
函数type返回类型

Lua 保留字:
and     break    do     else   elseif
end     false    for     function   if 
in     local    nil     not     or 
repeat   return   then   true   until  
while 


运算符:
< > <= >= == ~=

逻辑运算符:
and or not

连接运算符:
..

运算符优先级:
^ 
not  - (unary) 
*   / 
+   - 
.. 
<  >  <=  >=  ~=  == 
and 
or

table构造:
tab = {1,2,3}
tab[1]		访问容器从下标1开始

lua table构造list:
list = nil  
for  line in io.lines() do 
  list = {next=list,value=line} 
end

l = list 
while  l  do 
 print(l.value) 
  l = l.next 
end

赋值语句:
a,b = 10,2*x 
x,y = y,x

使用local 创建一个局部变量

if conditions then  
	then-part 
elseif conditions then 
	elseif-part 
..--->多个elseif 
else 
	else-part 
end;


while  condition do 
 statements; 
end ;

repeat 
 statements; 
until  conditions; 

for  var=exp1,exp2,exp3  do 
 loop-part 
end

for  i=10,1,-1 do 
 print(i) 
end

加载文件:
dofile	require	loadlib(加载库)	

pcall 在保护模式下调用他的第一个参数并运行,因此可以捕获所有的异常和错误。
如果没有异常和错误,pcall 返回true 和调用返回的任何值;否则返回nil加错误信息。 

local  status,err = pcall(function  () error( "my error" ) end ) 
print(err)


可以在任何时候调用debug.traceback 获取当前运行的 traceback 信息

多线程:
co = coroutine.create(function  ()
	for  i=1,10  do
		print("co",i)
		coroutine.yield()
	end
	end )

coroutine.resume(co)

print(coroutine.status(co))

coroutine.resume(co)
coroutine.resume(co)
coroutine.resume(co)

完整实例
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
	end
end

p = producer()

f = filter(p)
consumer(f)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读