一、安裝windows下的lua環境,luaforwindows
下載最新的5.3版本,下載地址:
https://sourceforge.net/projects/luabinaries/files/5.3.5/
也可以在gitlab下載:
https://github.com/rjpcomputing/luaforwindows/releases
下載解壓
lua53.exe就是lua語言解釋器
按住shift鼠標右鍵,此處打開命令窗口
編寫一個簡單的hello world程序
test.lua
如果覺得簡單,可以給一個for循環,打印1~10
正常執行,說明lua環境安裝沒有問題
二.lua語言的基本語法
- lua的值和類型
數值:
1,2,3
1.2 3.14
字符類型
"hello world"
Table類型
--lua的table,不需要預定義長度,保證數組在首次賦值的時候在最大索引加1
--沒有類型限制
--下標從1開始,自動擴容
--table = 數組+映射
--第一種寫法
a={}
a[1]=10
a[2]=20
a[3]="hello"
print(a[1],a[2],a[3])
--第二種寫法
a={
10,
20,
"hello"
}
print(a[1],a[2],a[3])
-- 映射的另外寫法
a = {}
a["hello"]=2
a[3]=false
print(a.hello, a[3])
--別的寫法
a={
["hello"]=2,
[3]=false,
}
print(a.hello,a[3])
- lua的函數
-- 加法函數
function add(a,b)
return a+b
end
--調用函數
print(add(1,2))
--調用函數不同的寫法
function add(a,b)
return a+b
end
a = add(1,2)
print(a)
--另外的寫法
add = function(a,b)
return a+b
end
foo =add
print(foo(1,2))
--函數返回多個值,返回加法和減法
function add(a,b)
return a+b,a-b
end
print(add(1,2))
- lua中多變量的賦值
a,b = 1,2
print(a,b)
--交換變量
a,b = b,a
-- 默認值為nil
a,b = 1
-- 多余的值會忽略
a,b = 1,2,3
- lua的表達式
a= 1 + 1
print(a)
a=1
b=2
print((b+2)*3)
-- lua沒有 ++
c = 1
c = c + 1
print(c)
5. lua的邏輯表達式
--and 兩個都為true,返回true,否則返回false
--or 任意一個為true,返回true
--not 取反
print(true and false)
6. lua拼接字符串,使用 兩個點號 ..
print("hello".." world")
7. --lua變量的范圍
-- 全局變量
function foo()
a = 1 -- 全局變量
end
foo()
print(a) -- 為1
-- 局部變量 一般使用local關鍵字修飾 ,我們在lua開發中應該盡量使用local局部變量
function foo()
local a = 1 -- 局部變量,local為作用域控制關鍵詞,分隔開,利於閱讀,還有優化作用
end
foo()
print(a) -- 為nil
8. lua的流程控制,判斷和循環
-- 老段子:老婆,包子和西瓜
-- 老婆打電話給程序員老公打了個電話:下班順路買一斤包子回來,如果看到賣西瓜的就買一個
-- 當天晚上回來程序員帶回來一個包子,老婆怒道:你怎么就買了一個包子,程序員:因為我看到了賣西瓜的啊
-- 當看到賣西瓜的就買一個包子,沒有看到賣西瓜的就買1公斤包子
if foundWatermelon() then
buy("baozi","1")
else
buy("baozi","1kg")
end
a.while循環,循環打印0-9
local i = 0
while i < 10 do
print(i)
i = i + 1
end
--lua的for數值遍歷
--for (int i = 0;i < 10; i ++)
--{
-- printf("%d\n",i);
--}
--打印1到10
for i = 1,10 do
print(i)
end
--等價於
for i = 1,10,1 do
print(i)
end
-- 倒敘打印
for i = 10,1,-1 do
print(i)
end
b. lua的for泛型遍歷
for的泛型遍歷: pairs() ipairs() 迭代器
-- pairs是內部的函數,功能就是可以把一個table里面的所有部分 全部遍歷
a= {
["foo"]=1,
[100]=true,
[1]=20,
[2]=30,
}
for k,v in pairs(a) do
print(k,v)
end
-- ipairs 功能變化了,只遍歷數組部分
a= {
["foo"]=1,
[100]=true,
[1]=20,
[2]=30,
}
for k,v in ipairs(a) do
print(k,v)
end
三、lua的包管理
-- lua的包(package)
local c = require("foo") -- 作為文件名搜索,默認搜索當前目錄
print(c)
print(c.foo(1,2))
-- foo.lua 的代碼
local class={}
function class.foo(a,b)
return a + b
end
-- 另外的寫法,class的映射:class.foo,foo鍵的 值為一個函數
--class.foo = function (a,b)
-- return a + b
--end
-- lua的一個特點,可以把全局代碼當做一個值返回
return class
-- 美化后
local socket = require("foo") -- 作為文件名搜索,默認搜索當前目錄
print(socket)
print(socket.send(1,2))
-- fooo.lua
local class={}
function class.send(a,b)
return a + b
end
-- 另外的寫法,class的映射:class.foo,foo鍵的 值為一個函數
--class.foo = function (a,b)
-- return a + b
--end
-- lua的一個特點,可以把全局代碼當做一個值返回
return class
-- require 加載文件,運行,推薦使用
-- dofile --加載並運行,可以多次加載,老的寫法
-- lua從5.3 以后沒有dostring功能了
print("require")
for i = 1,2 do
print(require("foo"))
end
print("dofile")
for i = 1,2 do
print(dofile("foo.lua"))
end
-- 運行結果
E:\software\lua5.3>lua53.exe test.lua
require
table: 008182a8 -- table相同,表示同一個
table: 008182a8
dofile
table: 00817c68
table: 0081e350
lua的系統庫
-- 批量插入table鍵值
local t = {}
for i = 1,10 do
table.insert(t,i)
end
for k,v in pairs(t) do
print(k, v)
end
print(t)
-- 刪除數組中的元素
table.remove(t,2)
-- 循環打印table 結果:
for k,v in pairs(table) do
print(k, v)
end
E:\software\lua5.3>lua53.exe test.lua
table: 00eb87d0
move function: 69e96240
sort function: 69e96ee0
insert function: 69e96720
remove function: 69e96550
concat function: 69e96890
unpack function: 69e95e30
pack function: 69e95fa0
--刪除元素的另外寫法
local t = {}
t.a = 1
t.b = 2
-- 刪除一個值
t.a = nil
for k,v in pairs(t) do
print(k, v)
end
獲取lua中對象的長度
-- # 可以獲取對象的長度,對象一般指的就是字符串和table
local t = {5, 1 ,3 ,4, 12}
local s = "hello world"
-- 得到table的長度
print(#t)
-- 取字符串的長度
print(#s)
-- type獲取類型
print(type(s))
-- 字符轉換為數字
local a = tonumber("3.14")
print(a, type(a))
-- 數字轉換為數字
local b = tostring(3.14)
print(b, type(b))
-- string.format 把一個字符串按照格式構造出來
print(string.format("hi %d", 20))
總結: