--lua基础
--单行注释
--[[]
段落注释
]]--
--引用其他lua文件,不需要后缀
--require"xx"
--变量不需要定义,可以直接赋值
count =1000 --没有分号分割符,成员变量
local max =111 --局部变量
--方法定义
function hello(...)
print("hello lua");
print(string.format(...));
end
--访问没有初始化的变量。lua will return nil
--call the function hello
hello("你know");
--pirnt the value of memeber
isOK = false
print(type(isOK))
--基本变量类型
a= nil
b =10
c =10.5
d = false
--定义字符串,单,双引号都可以
e = " i am mvp"
d = 'kevinLi'
--two strings can connect like this
stringA = "this is a "
stringB ="problem"
print(stringA..stringB);
--lua also support the transfer char
print(stringA.."\n"..stringB)
--modify the stringA about using gsub
stringA = string.gsub(stringA,"kevin","danel")
--字符和数字自动转换
stringC ="100"
stringC = tonumber(stringC)
stringC = stringC +20
stringC = tostring(stringC)
print(stringC.."\n".."nice lua")--..用于连接字符串
--取一个字符串长度使用
print(#stringC);
--lua only one struct that it is table
--create a table
tableA = {}
m ="x"
tableA[m] =100
m2 ='y'
tableA[m2] =200
print(tableA["x"].."\n"..tableA.y)
--table begin with1 index ,not zero
--table的数组下标从1开始
tableB = {"2","3","5"}
print(tableB[1])
--算术操作符
c1 =10+2
c2 =10-2
c3 =10*2
c4 =10/2
c5 =10^2
c6 =10%2
c7 =-10+2
print(c1.."_"..c2.."_"..c3.."_"..c4.."_"..c5.."_"..c6.."_"..c7)
--流程控制语句
--ifthen elseif thenelse end
abc =10
if abc ==10 then
print("V1")
elseif abc ==9 then
print("v2")
else
print("v2")
end
--循环控制语句
for i = begin,end,adddo
print("v2"..i+1)
end--end作为结束语句
--key ,value存储table
tableFor = {"kevin1","kevin2","kevin3"}
for k,vin pairs(tableFor) do
print("key:"..k.."value"..v)
end
//TODO:
--while
w1 =20
while true do
w1=w1+1
if w1 ==25then
break
end
end
print("whlile:"..w1)
--repeat
aa =20
repeat aa = aa+2
print("repeat:"..aa)
until aa>28
--关系操作符
--需要注意的是不等于符号 ~= 而不是!=
ax =10
bx =20
if ax >bxthen
print("GX1")
elseif ax<bxthen
print("GX2")
elseif ax>=bxthen
print("GX3")
elseif ax<=bxthen
print("GX4")
elseif ax==bxthen
print("GX5")
elseif ax~=bxthen
print("GX6")
else
print("GX7")
end
--next is the function
-- has a value returned
function funTest(abc)
local abc = abc+1
return abc
end
--have two or more values returned
function funTest2()
return2,2,3
end
a,b = funTest2()
print("a"..a.."b"..b)
--有变长参数的函数
function funTest3 (...)--...类似指针引用,lua区别与c++
print(...)
end
--闭合函数,子函数再母函数声明和实现
function funTest4(...)
print(...)
local a =2
a = a +2
function funTest5(...)
print(...)
end
funTest5(a)
end
funTest4(22)