編譯執行與錯誤
-- local name="張三"
-- if name~="張三"then
-- error("錯了")
-- end
--assert
(表示式);斷言
-- 如果表示式的值為假,整個程式將退出,並輸出一條錯誤資訊。如果表示式的值為真則繼續執行後面的語句。
local name = "李四"
local result = assert(name=="李3","你不是")
function text( )
print(f[1])
endif pcall(text)then print("正常")
else print("錯")
end函式test執行的時候肯定會報錯的,因為並不存在a這個table.
使用pcall呼叫test函式,如果test不報錯,則pcall返回ture,否則,返回false.
3.loadfile
loadfile-只編譯,不執行loadfi le故名思議,它只會載入檔案,編譯**,不會執行檔案裡的**。
比如,我們有乙個hello.lua檔案,當我們呼叫下面**的時候,loadfile("hello.lua"),不會輸出任何內容。
4.dofil le
dofile就是會執行**的傢伙了,如下**: dofile("./hello.lua");
5.require
require只執行一次,require和dofile有點像不過又很不一樣,require在第一次載入檔案
的時候會執行裡面的**。但是,第二次之後,再次載入檔案,則不會重複執行了。換句話說,它會儲存已經載入過的檔案,不會重複載入。
fori= 1,5 do
require("hello");
end詳細介紹 :loadfile會從檔案載入lua**但是不會執行**,最後將編譯的結果作為乙個函式返回,此外loadfile不會引|發錯誤,它只是返回錯誤值並不處理錯誤。
協同程式
local co = coroutine.create(functio print("hello coroutine"); end);
協同的程式的操作都在coroutine裡,create函式的引數就是協同程式要執行的函式就這
麼執行**是沒有效果的。
因為協同程式建立後,預設是掛起狀態。
協同程式的四種狀態分別為:掛起(suspended)、執行(running)\、死亡(dead)、正常(normal)。
要想協同程式執行起來,就要呼叫resume函式。
結果:hellolocal co = coroutine.create(function ( )
for i=1 ,2,1 do
print("hello");
-- 協同程式呼叫coroutine.yield()掛起
coroutine.yield()
endend )
-- 想要協同程式執行起來,就要呼叫coroutine.resume
coroutine.resume (co)
-- 列印協同狀態
print(coroutine.status(co))
coroutine.resume (co)
-- 從掛起的第二次,列印狀態
print(coroutine.status(co))
coroutine.resume (co)
-- dead死亡
print(coroutine.status(co))
suspended
hello
suspended
dead
這就是這段**的特殊之處了。resume返回兩個值第乙個值代表協同程式是否正常執行(true或false),第二個返回值自然是代表錯誤資訊。沒有錯誤就返回nil.
我們試試讓協同程式出現錯誤:
local co = coroutine.create(function ( )
-- error("錯1")
end )
local result ,msg=coroutine.resume(co)
print(result)
print(msg)
3.resume函式的引數
resume函式除了第乙個引數是協同程式外,第二個引數為「 resumeparame'
local co = coroutine.create(function (name )
print(name)
end )
coroutine.resume(co,222)結果:222
4.resume函式的第二個返回值
resume函式有兩個返回值。乙個代表協同呈序是否正確執行,乙個代表錯誤資訊。如果協同程式正確執行,錯誤資訊這個返回值自然就是 nil了.然後,這裡還有乙個規則,那就是yield函式的引數可以傳遞到resume的第二個返回值裡。
local co = coroutine.create(function (name )
print(name)
coroutine.yield("yield")
end )
local result ,msg=coroutine.resume(co,333)
print(msg) 執行結果:333 yield
5.yield函式的返回值
第一次執行的時候協同程式第一次被掛起,所以yield的返回要等待第二次resume 被呼叫時才能得到。
於是第二次呼叫resume時,首先就得到了上一次yield的返回值了,這個返回值正是resume的第二個引數。
沒錯 resu me的第二個返回值是yi eld的引數,而yield的返回值,是resu me的第二個引數。
再簡單一些,resume的返回值是yield的引數,yield的返回值是resu me的引數。
同時,resume的第二個引數也能傳遞給協同程式的函式。
local co = coroutine.create(function (name )
for i=1,3 do
print(name)
print("cd:" ..coroutine.yield("yield"))
end return "結束"
end )
for i=1,4 do
print("...d第" ..i .."執行")
local result ,msg=coroutine.resume(co,333)
print(msg)
end結果:
...d第1執行
333yield
...d第2執行
cd:333
333yield
...d第3執行
cd:333
333yield
...d第4執行
cd:333
nil(結束)
Lua 學習(2) Lua 基本語法
lua 提供了互動式程式設計模式。我們可以在命令列中輸入程式並立即檢視效果。lua 互動式程式設計模式可以通過命令 lua i 或 lua 來啟用 lua i lua 5.3.0 lua.org,puc rio 在命令列中,輸入以下命令 print hello world!接著我們按下回車鍵,輸出結...
Lua學習筆記(2) 基本語法
lua 有兩種程式設計模式 互動式程式設計 指令碼式程式設計。互動式程式設計 在命令列中輸入程式可立即看到結果。在 jetson tx1 的 tegar ubuntu terminal 裡面輸入 lua i或者 lua可以開啟lua環境。在命令列輸入並敲擊回車 print hello hello 指...
lua基本語法
第乙個字母可以是大小寫字母或者下劃線,其他位除上述之外可以是數字 mohd zara abc move name a 123 myname50 temp j a23b9 retval and break do else elseif end false for function if in loca...