Lua脚本语言基本语法快速入门教程
Lua语法与C语言有些相似也不太一样,写了语句学一下微笑
--begin
a=--[[explain]]"ha";
print(a)
ifa=="ha"then
print("iftestpassed")
else
print("ifusedwrong")
end
b,c=2,3
print(b,c)
b,c=c,b
ifb==3andc==2then
print("swaptestpassed")
print(b,c)
else
print("swaperror")
end
do
b=6
ifb==6then
print("codeblocktestpassed")
else
print("codeblocktesterror")
end
end
do
d=true;
locale="haha"
end
ifd==trueande==nilthen
print("localtestpassed")
else
print("localtesterror")
end
c=2^3
ifc==8then
print("squerttestpassed")
else
print("testerror")
end
a="stringwillbe".."connected"
print(a)
x=xora--ifnotxthenx=vend
print(x)
print(type(asdf))
c=3-1.2;
print(c)
d=[[
怎么会
怎么会
你竟原谅了我?
]]
print(d)
functiontest(w)
print("thenumis"..w)
localadd=w+1
returnadd;
end
b=test(5)
print(add,b)
t={
100,
[100]="I'mthe100thelement",
fsy=
{
['age']=22,
sex="male",--如果是字符串,可以去掉引号和括号
},--元素之间必须用,隔开
20,--相当于t[2]=20
}
print(t[0])
print(t[1])
print(t[100])
print(t.fsy.age)
print(t[2])
g={
age=3,
add=function(s,n)s.age=s.age+nend
}
g:add(10)--相当于g.add(g,10)
print(g.age)
运行的结果如下:
>lua-e"io.stdout:setvbuf'no'""hello.lua" ha iftestpassed 23 swaptestpassed 32 codeblocktestpassed localtestpassed squerttestpassed stringwillbeconnected stringwillbeconnected nil 1.8 怎么会 怎么会 你竟原谅了我? thenumis5 nil6 nil 100 I'mthe100thelement 22 20 13 >Exitcode:0