1.万年不变 hello world
print("hello world")
2.lua的数据类型
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({}))) --> table
3.if语句
if false or nil then
print("one true")
else
print("all false")
end
-->output:
all false
4.字符串定义
string1 = "hello"
string2 = 'world'
html = [[
<html>
<body>
hello world
</body>
</html>
]]
print(html)
str = string1.." "..string2
print(str) -->hello world
print("3"+5) -->8
print(#str) -->11
5.table(表)
tab1={}
a = {"apple","pear"}
a["key"]="orange"
key = 10
a[key]=22
a[key]=a[key]+11
for k,v in pairs(a) do
print(k..":"..v)
end
print("--")
for i=1,4 do
print(a[i])
end
-->output:
1:apple
2:pear
key:orange
10:33
--
apple
pear
nil
nil
6.function
function factorial(n,fun)
if( n==0) then
return 1
else
return n*factorial(n-1)
end
end
foo = factorial
print(foo(5))
-->output:
120