function fact(n)
    if n==0 then
        return 1
    else
        return n*fact(n-1)
    end
end

print("###",fact(5))

print("type('hello world')--",type("hello world"))
print("type(10)--",type(10))
print("type(3.14)--",type(3.14))
print("type(true)--",type(true))
print("type(type)--",type(type))
print("type(nil)--",type(nil))
对于字符串的操作需要根据需求重新创建字符串
a="one thing"
b=string.gsub(a,"one","another")
print(b)
数字字符串进行算数运算时自行转换为一个数字
print("10"-1)
x0="15"+1
print(x0)
.. 用于连接字符串,但是如果是数字时需要加空格否则会当成小数点处理
print(10 .. 20)
print("hello" .. 20)
虽然会自行转换为数字,但是“10”和10并不相等,如果需要将字符串转为数字可以使用tonumber
x1="10"
print(type(x1))
print(x1==10)
print(x1=="10")
x1=tonumber(x1)
print(type(x1))
需要将数字转为字符串需要使用tostring
x2=10
print(type(x2))
x2=tostring(x2)
print(type(x2))

x3=10
x3=10 ..""
print(type(x3))

x4="hello world"
字符串前加#用于求得字符串长度
print(#x4)
View Code

 

table实现了关联数组可以使用除了nil除外的任意类型值来进行索引(如果使用数字通常以1作为数组的起始值)
table无法声明,只能通过构造表达式创建,最简单的是{}
table0={}创建table,table0持有的是table引用,
table0=nil table0不在持有引用,table将会被llua回收器删除table
table0["x"]和table0.x是等价的
但是table0[x]和table0.x是不同的,table0.x是以字符串“x”索引,但是table0是以变量x索引,变量x可以被赋予任意值,如x="k",此时table0.k和table0[x]是等价的
 
lua 5.1后 符号#求table的长度如#table0
table0[#table0+1]="a"表示像table0列表末尾添加“a”
table0[#table0]=删除列表最后一个元素
table.insert(table,pos,value)插入
table.remove(table,pos)删除