lua 語法筆記

2021-10-05 02:12:51 字數 1625 閱讀 6357

建立檔案:helloworld.lua,內容如下:

print(「hello world!」)

執行指令碼:lua helloworld.lua

單行注釋(兩個減號):

– 注釋

多行注釋:

–[[

多行注釋

多行注釋

–]]

索引訪問:

t[i]

t.i 當索引為字串型別時的一種簡化寫法

gettable_event(t,i) 採用索引訪問,本質上是乙個類似這樣的函式呼叫

例:

site =

site[

"key"]=

"www.runoob.com"

print

(site[

"key"])

print

(site.key)

1、

while

(condition)

do statements

end

2、

for var=exp1,exp2,exp3 do

《執行體》

end

var 從 exp1 變化到 exp2,exp3 為 var 每次遞增步長,可選,如果不指定,預設為1。

例:

for i=1,

f(x)

doprint

(i)end

3、

repeat

statements

until

( condition )

if

( 布林表示式 1

)then

-- 語句 1

elseif

( 布林表示式 2

)then

-- 語句 2

elseif

( 布林表示式 3

)then

-- 語句 3

else

-- 語句 4

end

[local] function 函式名(引數1

, 引數2,.

.., 引數n)

函式體return

[value_1, value_2,..

., value_n]

end

注:

算數關係運算子:

關係運算子:

邏輯運算子:

其他運算子:

array =

mytable =

載入模組:

require 「《模組名》」

Lua 語法筆記 1

lua 是一種動態型別的語言。在語言中沒有型別定義的語法,每個值都 攜帶 了它自身的型別資訊。lua中有 8 種基礎型別 nil 空 boolean 布林 number 數字 string 字串 userdata 自定義型別 function 函式 thread 執行緒 和table 表 lua 中...

Lua學習筆記 基本語法

print hello world 輸出的結果為 hello world print hello world 這是單行注釋 print hello world 使用兩個減號與兩個中括號來進行多行注釋並以中括號結尾 lua表示符用來定義乙個變數,並且表示符的選取有相關規則。表示符一般使用字母或者下劃線...

Lua學習筆記 Lua入門基礎的語法

變數的定義 1.通常由a z或a z組成加下劃線 數字 2.也可以 開頭加小寫字母 3.變數區分大小寫全域性與區域性變數 1.使用local關鍵字定義區域性變數 2.未使用local關鍵字的預設為全域性變數 local a 123 a為區域性變數 b 567 b為全域性變數 注意多使用區域性變數 1...