溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Openresrt的Lua怎么搭建

發(fā)布時間:2021-12-27 17:44:01 來源:億速云 閱讀:152 作者:iii 欄目:大數(shù)據(jù)

本篇內(nèi)容介紹了“Openresrt的Lua怎么搭建”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

什么是lua

Lua 是一種輕量小巧的腳本語言,用標準C語言編寫并以源代碼形式開放, 其設(shè)計目的是為了嵌入應(yīng)用程序中,從而為應(yīng)用程序提供靈活的擴展和定制功能。


環(huán)境搭建

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ā)的同學,可以通過安裝插件的形式來集成Lua的環(huán)境,插件名為EmmyLua,安裝插件后,在Idea的右側(cè)欄就會出現(xiàn)Lua的圖標,點擊圖標,就會出現(xiàn)運行Lua代碼的窗口。建議使用該插件,可以免去安裝Lua環(huán)境的麻煩。

第一個Lua程序

安裝好環(huán)境后,我采用EmmyLua插件的形式,對Lua的入門語法進行一個簡單的講解。
打開EmmyLua的終端,在終端上輸入:

print("hi you")

按ctrl+enter,終端顯示:

hi you

Lua基本數(shù)據(jù)類型

lua的基本數(shù)據(jù)類型有nil、string、boolean、number、function類型。

nil 類型

nil類似于Java中的null ,表示空值。變量第一次賦值為nil。

local num
print(num)
num=100
print(num)

終端輸出:

nil

100

number (數(shù)字)

Number 類型用于表示實數(shù),和 Java里面的 double 類型很類似??梢允褂脭?shù)學函數(shù)
math.floor(向下取整) 和 math.ceil(向上取整) 進行取整操作。

local order = 3.99
local score = 98.01
print(math.floor(order))
print(math.ceil(score))

輸出:

3

99

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 類型實現(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, --相當于 [1] = 100876,此時索引為數(shù)字
            -- key = 1, value = 100876
    100191, --相當于 [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

function(函數(shù))

在 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

for 控制結(jié)構(gòu)

Lua 提供了一組傳統(tǒng)的、小巧的控制結(jié)構(gòu),包括用于條件判斷的 if 用于迭代的 while、repeat
和 for,本章節(jié)主要介紹 for 的使用.

for 數(shù)字型

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 泛型

泛型 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)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI