lua學習筆記11:lua中的小技巧,lua學習筆記

來源:互聯網
上載者:User

lua學習筆記11:lua中的小技巧,lua學習筆記

lua中的小技巧,即基礎lua語言本身的特種,進行一個些簡化的操作


一 巧用or

 x = x or v

等價於:

if not x then    x = vend

如果x為nil或false,就給他賦值為


二 三元運算子實現

a and b or c

類似C語言:

a ? b : c

and 的運算由優先順序高於or


三 不足位補0

str = string.format("%02d",num)

相當於

if num < 10 then    str = string.format("0%d", num)else    str = string.format("%d", num)end

這種表達常用於得到有規律的圖片名稱,時間的轉換等


lua 中的賦值問題與type方法

print(123)函數用來向標準輸出輸出資訊。print是沒有傳回值的。
所以你的a=print(123)
沒有對a產生任何影響。它依然不存在。讀取任何不存在的變數都會得到nil。
type函數參數為nil時,返回nil。所以type(a)==type(nil)==nil
度的對於type(a) 會返回nil,nil是lua內建類型,表示不存在的值。
 
LUA菜鳥在初安裝LUA遇到的問題解

windows 下面不用這麼麻煩,有編譯好的安裝包直接用行了:
luaforge.net/frs/?group_id=377
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.