重慶分公司,新征程啟航
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)
本篇內(nèi)容介紹了“Openresrt的Lua怎么搭建”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
成都網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、成都網(wǎng)站建設(shè)、微信開發(fā)、小程序設(shè)計、集團成都企業(yè)網(wǎng)站定制等服務(wù)項目。核心團隊均擁有互聯(lián)網(wǎng)行業(yè)多年經(jīng)驗,服務(wù)眾多知名企業(yè)客戶;涵蓋的客戶類型包括:衛(wèi)生間隔斷等眾多領(lǐng)域,積累了大量豐富的經(jīng)驗,同時也獲得了客戶的一致贊揚!
Lua 是一種輕量小巧的腳本語言,用標(biāo)準(zhǔn)C語言編寫并以源代碼形式開放, 其設(shè)計目的是為了嵌入應(yīng)用程序中,從而為應(yīng)用程序提供靈活的擴展和定制功能。
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開發(fā)的同學(xué),可以通過安裝插件的形式來集成Lua的環(huán)境,插件名為EmmyLua,安裝插件后,在Idea的右側(cè)欄就會出現(xiàn)Lua的圖標(biāo),點擊圖標(biāo),就會出現(xiàn)運行Lua代碼的窗口。建議使用該插件,可以免去安裝Lua環(huán)境的麻煩。
安裝好環(huán)境后,我采用EmmyLua插件的形式,對Lua的入門語法進行一個簡單的講解。
打開EmmyLua的終端,在終端上輸入:
print("hi you")
按ctrl+enter,終端顯示:
hi you
lua的基本數(shù)據(jù)類型有nil、string、boolean、number、function類型。
nil類似于Java中的null ,表示空值。變量第一次賦值為nil。
local num print(num) num=100 print(num)
終端輸出:
nil
100
Number 類型用于表示實數(shù),和 Java里面的 double 類型很類似。可以使用數(shù)學(xué)函數(shù)
math.floor(向下取整) 和 math.ceil(向上取整) 進行取整操作。
local order = 3.99 local score = 98.01 print(math.floor(order)) print(math.ceil(score))
輸出:
3
99
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 類型實現(xiàn)了一種抽象的“關(guān)聯(lián)數(shù)組”。“關(guān)聯(lián)數(shù)組”是一種具有特殊索引方式的數(shù)組,索引通常是字符串(string) 或者 number 類型,但也可以是除 nil 以外的任意類型的值。
local corp = { web = "www.google.com", --索引為字符串,key = "web", -- value = "www.google.com" telephone = "12345678", --索引為字符串 staff = {"Jack", "Scott", "Gary"}, --索引為字符串,值也是一個表 100876, --相當(dāng)于 [1] = 100876,此時索引為數(shù)字 -- key = 1, value = 100876 100191, --相當(dāng)于 [2] = 100191,此時索引為數(shù)字 [10] = 360, --直接把數(shù)字索引給出 ["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
在 Lua 中,函數(shù) 也是一種數(shù)據(jù)類型,函數(shù)可以存儲在變量中,可以通過參數(shù)傳遞給其他函
數(shù),還可以作為其他函數(shù)的返回值。
local function foo() print("in the function") --dosomething() local x = 10 local y = 20 return x + y end local a = foo --把函數(shù)賦給變量 print(a()) --output: in the function 30
~= 不等于
邏輯運算符 | 說明 |
---|---|
and | 邏輯與 |
or | 邏輯或 |
not | 邏輯非 |
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
Lua 提供了一組傳統(tǒng)的、小巧的控制結(jié)構(gòu),包括用于條件判斷的 if 用于迭代的 while、repeat
和 for,本章節(jié)主要介紹 for 的使用.
for 語句有兩種形式:數(shù)字 for(numeric for) 和范型 for(generic for) 。
數(shù)字型 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 循環(huán)通過一個迭代器(iterator) 函數(shù)來遍歷所有值:
-- 打印數(shù)組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
“Openresrt的Lua怎么搭建”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!