lua基础篇1

所有的变量默认都是全局变量,
变量区分大小写
变量可以由数字字母组成,但是不能以数字开头
字符串定义如果有单双引号需要用“\”转义,或者使用xyz = [[define string like: 'x' and "tt" ]]; print(xyz)--以这种方式的话,连其中定义的回车和换行都可以输出
变量赋值是按照字段顺序赋值,比如a,b,c,d=1,2,"xyz","mpo"; print (a,b,c,d) ==>1 2 xyz mpo;
变量交换很直接: a,b=1,2; print(a,b); a,b=b,a; print(a,b) ==> 2 1
操作符"..",连接字符串的作用 TODO
输出的方式 print "hello lua" == print("hello lua") != io.write("hello lua") 因为io.write不会输出换行,而print()会。

table的用法:
a={}
b={1,2,3}
c={"x","y","z"}
print(a,b,c)这个只能输出a,b,c的地址而已。

table的赋值和输出
newTbl={};
newTbl.name = "hello"
newTbl.sex="male"
print(newTbl.name, newTbl["sex"])  ==> hello  male

条件语句:
a= 1
if a==1 then
    print("a is printed")
else if a==x then
    do opr...
else

end

b=(a==1) and "one" or "two" ==>简单的条件语句组合,如果a==1则b="one" 否则 b="two"


循环的使用:
~=:逻辑表达式中的不等于
a=0;
while a~= 5 do
    a=a+1
    io.write(a.." ")
end
和可以写成:
repeat
    a=a+1
    print(a)
until a==5

for a=1,4 do ==>这个相当于 a=1; a<=4; a++
    io.write (a)
end

for a=1,6,3 do ==>这个相当于 a=1; a<=6 step=3
    io.write(a)
end

有序的键值对,序号是从1开始
for key, values in pairs({1,2,3,4}) do print key,values end
a={1,2,3,4,"five", "six", "seven"}
for i,v in pairs(a) do print (i, v) end;


方法的调用:
function myFirstLuaFunction()
    print("sth...")
    
    return "test string"
end

--这样调用
myFirstLuaFunction();


方法调用输入参数可以不匹配的:
a=myFirstLuaFunction("test sth...");
print(a)  ==> test string

方法返回多个参数:
function mySecondLuaFunction(a, b, c)
    return a, b, c, "string1", "string2"
end

x,y,z,m,n=mySecondLuaFunction(1,2,3)
print(x,y,z,m,n) ==> 1 2 3 string1 string2

printf的实现:---> 这里可以说明了格式化字符串的使用 string.format(fmt, ...)
function printf(fmt, ...)
    io.write(string.format(fmt, ...))
end
printf("Test a[%d] and b[%d]", a, b) ==> testString=string.format("Test a[%d] and b[%d]", a, b)



标准库的使用
math, string, table, input/output, GUI, socket, XML and many more...
 Math functions:
 math.abs, math.acos, math.asin, math.atan, math.atan2,
 math.ceil, math.cos, math.cosh, math.deg, math.exp, math.floor,
 math.fmod, math.frexp, math.huge, math.ldexp, math.log, math.log10,
 math.max, math.min, math.modf, math.pi, math.pow, math.rad,
 math.random, math.randomseed, math.sin, math.sinh, math.sqrt,
 math.tan, math.tanh
 
 
-- String functions:
-- string.byte, string.char, string.dump, string.find, string.format,
-- string.gfind, string.gsub, string.len, string.lower, string.match,
-- string.rep, string.reverse, string.sub, string.upper
print(string.upper("lower"),string.rep("a",5),string.find("abcde", "cd"))


-- Table functions:
-- table.concat, table.insert, table.maxn, table.remove, table.sort
a={2}
table.insert(a,3);
table.insert(a,4);
table.sort(a,function(v1,v2) return v1 > v2 end)
for i,v in ipairs(a) do print(i,v) end
-------- Output ------
1       4
2       3
3       2



IO functions:
-- io.close , io.flush, io.input, io.lines, io.open, io.output, io.popen,
-- io.read, io.stderr, io.stdin, io.stdout, io.tmpfile, io.type, io.write,
-- file:close, file:flush, file:lines ,file:read,
-- file:seek, file:setvbuf, file:write
print(io.open("file doesn't exist", "r"))
-------- Output ------
nil     file doesn't exist: No such file or directory   2



OS functions:
-- os.clock, os.date, os.difftime, os.execute, os.exit, os.getenv,
-- os.remove, os.rename, os.setlocale, os.time, os.tmpname
print(os.date())
-------- Output ------
04/22/13 09:52:38

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值