Openresrt最佳案例|第2篇:Lua入門
作者:方誌朋
原文:https://blog.csdn.net/forezp/article/details/78616622
什麼是lua
Lua 是一種輕量小巧的腳本語言,用標準C語言編寫並以源代碼形式開放, 其設計目的是爲了嵌入應用程序中,從而爲應用程序提供靈活的擴展和定製功能。
Lua 是巴西里約熱內盧天主教大學(Pontifical Catholic University of Rio de Janeiro)裏的一個研究小組,由Roberto Ierusalimschy、Waldemar Celes 和 Luiz Henrique de Figueiredo所組成並於1993年開發。
—-摘抄 http://www.runoob.com/lua/lua-tutorial.html
環境搭建
注意: 在上一篇文章中,OpenResty已經有了Lua的環境,這裏安裝的是單獨的Lua環境,用於學習和開發Lua。大多數的電腦是Windowds版本的電腦,Windows版本下載地址http://luaforge.net/projects/luaforwindows/。
Linux和Mac電腦下載地址:http://luajit.org/download.html,安裝命令如下:
wget http://luajit.org/download/LuaJIT-2.1.0-beta1.tar.gz
tar -xvf LuaJIT-2.1.0-beta1.tar.gz
cd LuaJIT-2.1.0-beta1
make
sudo make install
使用IDEA開發的同學,可以通過安裝插件的形式來集成Lua的環境,插件名爲EmmyLua,安裝插件後,在Idea的右側欄就會出現Lua的圖標,點擊圖標,就會出現運行Lua代碼的窗口。建議使用該插件,可以免去安裝Lua環境的麻煩。
第一個Lua程序
安裝好環境後,我採用EmmyLua插件的形式,對Lua的入門語法進行一個簡單的講解。
打開EmmyLua的終端,在終端上輸入:
print("hi you")
按ctrl+enter,終端顯示:
hi you
Lua基本數據類型
lua的基本數據類型有nil、string、boolean、number、function類型。
nil 類型
nil類似於Java中的null ,表示空值。變量第一次賦值爲nil。
local num
print(num)
num=100
print(num)
終端輸出:
nil
100
number (數字)
Number 類型用於表示實數,和 Java裏面的 double 類型很類似。可以使用數學函數
math.floor(向下取整) 和 math.ceil(向上取整) 進行取整操作。
local order = 3.99
local score = 98.01
print(math.floor(order))
print(math.ceil(score))
輸出:
399
string 字符串
Lua 中有三種方式表示字符串:
1、使用一對匹配的單引號。例:’hello’。
2、使用一對匹配的雙引號。例:”abclua
3.字符串還可以用一種長括號(即[[ ]]) 括起來的方式定義
ocal str1 = 'hello world'
local str2 = "hello lua"
local str3 = [["add\name",'hello']]
local str4 = [=[string have a [[]].]=]
print(str1) -->output:hello world
print(str2) -->output:hello lua
print(str3) -->output:"add\name",'hello'
print(str4) --
table (表)
Table 類型實現了一種抽象的“關聯數組”。“關聯數組”是一種具有特殊索引方式的數組,索引通常是字符串(string) 或者 number 類型,但也可以是除 nil 以外的任意類型的值。
local corp = {
web = "www.google.com", --索引爲字符串,key = "web",
-- value = "www.google.com"
telephone = "12345678", --索引爲字符串
staff = {"Jack", "Scott", "Gary"}, --索引爲字符串,值也是一個表
100876, --相當於 [1] = 100876,此時索引爲數字
-- key = 1, value = 100876
100191, --相當於 [2] = 100191,此時索引爲數字
[10] = 360, --直接把數字索引給出
["city"] = "Beijing" --索引爲字符串
}
print(corp.web) -->output:www.google.com
print(corp["telephone"]) -->output:12345678
print(corp[2]) -->output:100191
print(corp["city"]) -->output:"Beijing"
print(corp.staff[1]) -->output:Jack
print(corp[10]) -->output:36
function(函數)
在 Lua 中,函數 也是一種數據類型,函數可以存儲在變量中,可以通過參數傳遞給其他函
數,還可以作爲其他函數的返回值。
local function foo()
print("in the function")
--dosomething()
local x = 10
local y = 20
return x + y
end
local a = foo --把函數賦給變量
print(a())
--output:
in the function
30
表達式
~= 不等於
- a and b 如果 a 爲 nil,則返回 a,否則返回 b;
- a or b 如果 a 爲 nil,則返回 b,否則返回 a。
local c = nil
local d = 0
local e = 100
print(c and d) -->打印 nil
print(c and e) -->打印 nil
print(d and e) -->打印 100
print(c or d) -->打印 0
print(c or e) -->打印 100
print(not c) -->打印 true
print(not d) --> 打印 false
在 Lua 中連接兩個字符串,可以使用操作符“..”(兩個點).
print("Hello " .. "World") -->打印 Hello World
print(0 .. 1) -->打印 01
控制語句
單個 if 分支 型
x = 10
if x > 0 then
print("x is a positive number")
end
兩個分支 if-else 型
x = 10
if x > 0 then
print("x is a positive number")
else
print("x is a non-positive number")
end
多個分支 if-elseif-else 型:
score = 90
if score == 100 then
print("Very good!Your score is 100")
elseif score >= 60 then
print("Congratulations, you have passed it,your score greater or equal to 60")
--此處可以添加多個elseif
else
print("Sorry, you do not pass the exam! ")
end
for 控制結構
Lua 提供了一組傳統的、小巧的控制結構,包括用於條件判斷的 if 用於迭代的 while、repeat
和 for,本章節主要介紹 for 的使用.
for 數字型
for 語句有兩種形式:數字 for(numeric for) 和範型 for(generic for) 。
數字型 for 的語法如下:
for var = begin, finish, step do
--body
end
實例1:
for i = 1, 5 do
print(i)
end
-- output:
1 2 3 4 5
實例2:
for i = 1, 10, 2 do
print(i)
end
-- output:
1 3 5 7 9
for 泛型
泛型 for 循環通過一個迭代器(iterator) 函數來遍歷所有值:
-- 打印數組a的所有值
local a = {"a", "b", "c", "d"}
for i, v in ipairs(a) do
print("index:", i, " value:", v)
end
-- output:
index: 1 value: a
index: 2 value: b
index: 3 value: c
index: 4 value: d
lua的入門就到這裏,因爲lua語法雖少,但細節有很多,不可能花很多時間去研究這個。入個門,遇到問題再去查資料就行了。另外需要說明的是本文大部分內容爲複製粘貼於OPenResty 最佳實踐,感謝原作者的開源電子書,讓我獲益匪淺。更多內容請參考:
lua入門教程:http://www.runoob.com/lua/lua-tutorial.html
OPenResty 最佳實踐: https://moonbingbing.gitbooks.io/openresty-best-practices/content/index.html