1)先来个helloworld:


--注释是这么写的!
print 'helloWorld'
--输出结果:helloWorld
a='hello'
print(a)
--输出结果:hello


2)主要类型:


a=1
b="abc"
c={}
d=print
print(type(a))
--输出结果:number
print(type(b))
--输出结果:string
print(type(c))
--输出结果:table
print(type(d))
--输出结果:function


函数类型的变量,加上"() "即可以执行,如:


d(b)
--输出结果:abc


3)变量及常量,字符串:


a,b,c,d = 1,2,'c',{1}
print (a,b,c,d)
-- 输出结果:1       2       c       table: 0041BC58
a="single 'quoted' string and double \"quoted\" string inside"
b='single \'quoted\' string and double "quoted" string inside'
c= [[
multiple line
with'single'
and "double" quoted strings inside。
]]
print(a)
--输出结果:single 'quoted' string and double "quoted" string inside
print(b)
--输出结果:single 'quoted' string and double "quoted" string inside
print(c)
--输出结果:
--multiple line
--with'single'
--and "double" quoted strings inside。
--字符串用两个..可以进行连接
a=a.."字符串连接"
print(a)
--single 'quoted' string and double "quoted" string inside。字符串连接


我们关注一下lua比较特别的地方

  • 声明变量及给变量赋值的时候,可以多变量一起赋值

  • 字符串常量可以用双引号及单引号,并且混用的时候无须转义字符,跟php有点类型,还可以用"[[]]"括起来,
    这一点类似于c#里面字符串可以加上@符号,可以保留字符串内的格式


4)逻辑控制语句:


--逻辑控制语句
a=168
if a==168 then
    print("一路发")
elseif a== 1314 then
    print("一生一世")
else
    print("数字语言")
end
--输出结果:一路发


5)循环结构:

第一种:


a=1
result = ""
while a~=10 do
    result = result..a.."  "
    a=a+1
end
print(result)
--输出结果:1  2  3  4  5  6  7  8  9


第二种:


--repeat、until循环
a=0
result = ""
repeat
      a=a+1
      result = result..a.."  "
until a==9
print(result)
--输出结果:1  2  3  4  5  6  7  8  9


第三种:


--第三种for循环
result = ""
for a=1,4 do
    result = result..a.." "
end
print(result)
--输出结果:1 2 3 4


for循环有个步进的概念,默认为1,以下例子步进为2:


result = ""
for a=1,10,2 do
    result = result..a.." "
end
print(result)
--输出结果:1 3 5 7 9


6)函数及其使用:


--1.先来个最简单的
function myFunc1()
    print("hello function")
end
myFunc1()
--输出结果:hello function
--2.再来个带参数的
a=1
b=2
function myFunc2(par1,par2)
    return par1+par2
end
c=myFunc2(a,b)
print(c)
--输出结果:3
--3.甚至可以返回多个变量
a=1
b=2
function myFunc3(A,B)
    return B,A
end
c,d=myFunc3(a,b) --当然如果给多个变量赋值就没有问题了
print(c,d)
--输出结果:2       1
print(myFunc3(a,b)) --直接打印就把所有打印出来
--输出结果:2       1
--4.还可以是变参
function myFunc4(...)
    print(...)
end
myFunc4(a,b,c,d)
--输出结果:1       2       2       1


7)table(理解为可变参数object)的使用:


person={}
person.sex = "男"
person.age = 27
print(person," "..person.sex," "..person["age"])
--输出结果:table: 01405AB0 男      27
A=person
print(A["sex"])
--输出结果:男
print(A)  --引用类型,看A以及linbc内存地址是一样的
--输出结果:table: 01405AB0
for i,v in pairs(person) do
    print(i.."==>"..v)
end
--输出结果:
--age==>27
--sex==>男
--也可以作为数组,下标是从1开始的喔
arr={1,2,3,"a",'b',person}
for i,v in pairs(arr) do
   print(i," ",v)
end
--输出结果:
--1       1
--2       2
--3       3
--4       a
--5       b
--6       table: 01405AB0
--既然作为数组吧,那肯定是可以增加记录的吧
table.insert(arr,A)
table.foreach(arr,function(i,v) print(v) end)
--输出结果:
--1
--2
--3
--a
--b
--table: 01405AB0
--table: 01405AB0
--当然也可以用下标访问了
print(arr[1])
--输出结果:1
--调用table.insert会自动给增加下标,如
table.insert(person,"hello")
print(arr[1])
--输出结果:1