Lua編程入門-學習筆記1

來源:互聯網
上載者:User

標籤:

第1章:起點

Chunks: 語句塊

每個語句結尾的分號是可選的,如果同一行有多個語句最好使用分號分隔;

dofile("lib1.lua")  -- 執行lua檔案

全域變數:局部變數用local修飾,否則就是全域變數

保留字:

        if then else elseif end

        and or not

        fuction return end

        true false nil

        while ... do .. end    break

        repeat ... until ...

        for ... in ... do ... end

        local

 

注釋:單行 --

         多行 --[[ XXX  --]]

命令列: lua [option] [script [args]]

       -e: 直接傳入命令,如:lua -e "print(math.sin(12))"

       -l: 負載檔案,如:lua -la -lb,執行a.lua和b.lua檔案

       -i: 互動模式,

 

第2章 類型和值

8個基本類型: nil\boolean\number\string\userdata\function\thread\table

通過type()擷取類型

逸出字元: \[  \]

 

第3章 運算式

算術運算子

      二元運算子: + - * / ^ (加減乘除冪)

      一元運算子: -

關係運算子

       <  >  <=  >=  ==  ~=

       nil只和自己相等

邏輯運算子

        and or not

        a ? b : c

串連運算子

         ..    -> string

表

{}   => days = {"sunday", "monday"}

            days[1]  索引從1開始!!!

a = {x=0, y=1}   <=>  a={}; a.x=0; a.y=1  <=>  {["x"]=0, ["y"]=1}

{"red", "green", "blue"}    <=>  {[1]="red", [2]="green", [3]="blue"}

不推薦數組下標從0開始,否則很多標準庫不能使用

 

第4章 基本文法

指派陳述式:  x,y = y,x

do ... end   <=>  C++的{}

迴圈語句:

while condition do

     statements

end

repeat

     statements

until conditions;

for var=exp1, exp2, exp3 do  --注意:exp1,exp2,exp3隻會計算一次!!!

    loop-part

end 

for i,v in ipairs(a) do print(v) end

 

第5章 函數

 調用函數的時候,如果參數列表為空白,必須使用()表明是函數調用,當函數只有一個參數並且這個參數是字串或者表構造的時候,()可有可無。

Lua函數實參和形參的匹配與指派陳述式類似,多餘部分被忽略,缺少部分用nil補足

特殊函數unpack,數組作為參數返回數組的每個元素

可變參數:  func(...)

    Lua將函數的可變參數放在一個叫arg的表中,除了參數以外,arg表中還有一個域n表示參數的個數

啞元變數:  _

local _, x = string.find(s, p)

具名引數:

rename(old="temp.lua", new="temp1.lua")

Lua編程入門-學習筆記1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.