#!/bin/lua
--lua脚本一旦出错不会再往下运行
print("Hello World!") --输出
print(type("Hello world")) --> string
print(type(10.4*3)) --> number
print(type(print)) --> function
print(type(type)) --> function
print(type(true)) --> boolean
print(type(nil)) --> nil
print(type(type(X))) --> string
print("table类型,可以混合array和map,遍历时先遍历array类型,index从1开始")
tab1 = { key1 = "val1", key2 = "val2", "val3"}
print(tab1,#tab1) --table: 0x77f680 1 #号只计算数组长度
for k, v in pairs(tab1) do
print(k .. " - " .. v) -- ..是字符串连接运算符
end
print("赋值为nil即删除key")
tab1.key1 = nil
for k, v in pairs(tab1) do
print(k .. " - " .. v)
end
--print(tab1["key1"] .. "," .. tab1["key2"]) --可以用[]访问 nil 用..连接保错
--print(tab1.key1 .. "," .. tab1["key2"]) --可以用.访问
print(tab1["key1"]) --nil
print(tab1["key2"]) --val2
print(tab1.key1) --nil
print(tab1.key2) --val2
print("globalVal:",globalVal,"localVal:",localVal)
function test()
globalVal = 5 --全局变量
local localVal = 6 --局部变量
end
test()
print("globalVal:",globalVal,"localVal:",localVal)
globalVal,localVal = localVal,globalVal --多个变量同时赋值
print("globalVal:",globalVal,"localVal:",localVal)
tmp1,tmp2,tmp3 = 1,"2" --右边不足补nil,多了忽略
print(tmp1,tmp2,tmp3)
--[ 0 为 true ] false和nil 为false
if(false)
then
print("false 不进入")
elseif(nil)
then
print("nil 不进入")
elseif(0)
then
print("0 为 true")
end
--特殊运算符
print("2的10次方",2^10) -- ^为乘幂
print("不等于",1~=2,2~=3,2~=2) -- ~=为不等于
print("aaa" .. 12) -- ..为字符串连接运算符,数字可直接当运算符
print("12"+12) --字符串可直接转化为数字计算
--print("aaa"+12) --字符串转换失败会报错
print(#"aaaaa") -- #返回字符串长度
--[[运算符优先级,除了^和..外所有的二元运算符都是左连接的。
^
not - (取负)
* /
+ -
..
< > <= >= ~= ==
and
or
]]--
print(4 .. 5^2+1) -- 4 .. ((5^2)+1) = 4 .. (25+1) = 4 .. 26.0 = 426.0
--定义字符串
str = "\"lua string\""
print(str)
str = '\'lua string\''
print(str)
str = [[
"lua
string"
]] --多行字符串
print(str)
function add(a,b,funPrint)
funPrint(a,b)
return a,b,a+b --可使用多值返回
end
myprint = function(a,b)
print(a,"+",b,"=",a+b)
end
add(2,3,myprint)
add(2,3,
function(a,b)
print(a,b,a+b)
end
)
print(add(2,3,print))
--可变传参
function average(...)
result = 0
local arg = {...}
for i,v in ipairs(arg) do
result = result + v
end
print("总共传入 " .. #arg .. " 个数")
return result/#arg
end
print(average(1,2,3,4,5))
i=3
while(i>0)
do
print("while i的值为:",i)
i = i-1
end
repeat
print("repeat i的值为:",i)
i = i+1
until(i>3)
for i=3,0,-1 do -- 3以-1为步来增涨到0,如过-1的位置不赋值,默认为1
print("for i的值为:",i)
end
function f(x)
print("f(" .. x .. ")")
return x*3
end
for i=1,f(i) do --此时f(i)传入的i值为上面算出来的i=4,且f(i)只算一次
print("for f(i) i的值为:",i)
end
运行结果:
Hello World!
string
number
function
function
boolean
nil
string
table类型,可以混合array和map,遍历时先遍历array类型,index从1开始
table: 0x1aa3690 1
1 - val3
key2 - val2
key1 - val1
赋值为nil即删除key
1 - val3
key2 - val2
nil
val2
nil
val2
globalVal: nil localVal: nil
globalVal: 5 localVal: nil
globalVal: nil localVal: 5
1 2 nil
0 为 true
2的10次方 1024.0
不等于 true true false
aaa12
24.0
5
426.0
"lua string"
'lua string'
"lua
string"
2 + 3 = 5
2 3 5
2 3
2 3 5
总共传入 5 个数
3.0
while i的值为: 3
while i的值为: 2
while i的值为: 1
repeat i的值为: 0
repeat i的值为: 1
repeat i的值为: 2
repeat i的值为: 3
for i的值为: 3
for i的值为: 2
for i的值为: 1
for i的值为: 0
f(4)
for f(i) i的值为: 1
for f(i) i的值为: 2
for f(i) i的值为: 3
for f(i) i的值为: 4
for f(i) i的值为: 5
for f(i) i的值为: 6
for f(i) i的值为: 7
for f(i) i的值为: 8
for f(i) i的值为: 9
for f(i) i的值为: 10
for f(i) i的值为: 11
for f(i) i的值为: 12