30分钟入门Lua

来源:互联网 发布:网络银商 编辑:程序博客网 时间:2024/05/22 12:33
废话少说,直接撸代码print("hello")--[[注释测试]]a=1b="abc"c={}d=printprint(type(a))print(type(b))print(type(c))print(type(d))one_two_3 = 123--[[可以使用变量名]]--[[1_two_3 = 456不可以使用变量名]]print(_VERSION)--[[在Lua中以下划线开头通常为特殊值]]ab = 1Ab = 2AB =3print(ab,Ab,AB)--[[Lua大小写敏感]]--[[lau中关键字,例如:and,break,do,else,elseif,end,false,for,function,if,local,nil,not,or,repeat,return,then,true,until,while不能用作变量名]]AND = 500--[[虽然and不可以,但是AND可以作为变量名]]print(AND)--example 8 --字符串a = "single 'quoted' string and double \"quoted\" string inside"b = 'single \'quoted\' string and double "quoted" string inside'c =[[multiple linewith 'single'and "double" quoted string inside.]]print(a)print(b)print(c)--example 9 --Assignments--多个任务是可用的a,b,c,d = 1, 2 , "three" , "four"print(a)print(b)print(c)print(d)--example 10 --更多任务print(a,b)a,b = b,aprint(a,b)--example 11 --Numbers--两个逗点(..)被用来连接后边stringsa,b,c,d,e = 1,1.123,1E9,-123,.0008print("a=" ..a,"b="..b,"c="..c,"d="..d,"e="..e)--example --More Outputprint "Hello Lua Boy"print "Hello Lua Gil"--example --More More Outputio.write("Hello lua dad--|")io.write("|-Hello lua mom")print()--用一个空的输出重写到一行--example 14 --Tables--创建简单的表a={} --{} 创建一个空表b={1,2,3} --创建一个包含1,2,3的表c={"a","b","c"} --创建一个包含字符串a,b,c的表print(a,b,c)--example 15 --More Tablesaddress ={} --空表address.Street = "Lodon Street"address.StreetNumber = 6688address.City = "Lodon"print(address.StreetNumber,address["City"],address.Street)--example 16 --if语句a = 1if a==1 thenprint("a is One")end--example 17 --if else语句b = "Happy"if b == "Sad" thenprint("b is Sad")elseprint("b is not Sad")end--example 18 --if elseif else语句c = 3if c== 1 thenprint("c is 1")elseif c==2 thenprint("c is 2")elseprint("c is 3")end--example 19 --条件赋值a=1m = (a==1) and "one" or "not one"print(m)--等同于a= 1if a == 1 thenm = "one"elseb = "not one"end--example 20 --while statementa =1while a~=5 do--Lua Uses ~= 意味着不相等a=a+1io.write(a.." ")end--example 21 --repeat until 语句a= 0repeata=a+1print(a)until a==5--example 22 --for 语句for a=1,4 do --自增1io.write(a)endprint()for a=1,6,3 do --自增3io.write(a)endprint()--example 23 --More for statement--连续的迭代形式for key,value in pairs({1,2,3,4}) doprint(key,value)end--example 24 --打印表a={1,2,3,4,"five","elephant","mouse"}for i,sb in pairs(a) doprint(i,sb)end--example 25 --break 语句--break 用来跳出循环a=0while true doa=a+1if a==10 thenbreakendendprint(a)--example 26 --函数function myFirstLuaTunction()--不包含参数和返回值的函数print("我的第一个Lua函数被执行")end--执行myFirstLuaTunction()--example 27 --更多函数function mySecondLuaFunction()--有返回值的函数return "string from my sencond function"--return "123"end--a=mySecondLuaFunction("string");--a=mySecondLuaFunction("Numbers")a=mySecondLuaFunction();print(a)--example 28 --更多更多函数function myFirstLuaFunctionWithMultipleReturnValues(a,b,c) return a,b,c,"My first lua function with multiple return values", 1, trueenda,b,c,d,e,f = myFirstLuaFunctionWithMultipleReturnValues(1,2,"three")print(a,b,c,d,e,f)--example 29 --变量功能和范围--所有变量默认为全局b="global"--要设置局部变量,必须加关键字localfunction myfunc()local b = "local variable"a ="global variable"print(a,b)endmyfunc()print(a,b)--example 30 --格式化打印--printf的实现--os.getenv"USER" 返回过程环境变量,如果没有定义则为空--os.date() 包含日期和时间--_VERSION 版本号function printf(fmt, ...) io.write(string.format(fmt, ...))endprintf("Hello %s from %s on %s\n", os.getenv"USER" or "there", _VERSION, os.date()) print(os.getenv"USER")--example 31 --上下协调--[[Standard Libraries(标准库)Lua便准内置库,在常见操作方面,例如:数学,字符串,表,输入输出以及操作系统设施External Libraries(外部库)包括套接字,XML,性能分析,日志,单元测试,GUI工具包,web框架等等很多]]--example 32 --标准库 for math-- 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.tanhprint(math.sqrt(9),math.pi)--example 33 --标准库 for string-- 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.upperprint(string.upper("lower"),string.rep("a",5),string.find("abcde","cd"))--example 34 --标准库 for table-- Table functions:-- table.concat, table.insert, table.maxn, table.remove, table.sorta={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--example 35 --标准库 for input/output-- 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:writeprint(io.open("file doesn't exits","r"))--r为读模式,default模式--example 36 --标准库 for operating system facilities-- OS functions:-- os.clock, os.date, os.difftime, os.execute, os.exit, os.getenv,-- os.remove, os.rename, os.setlocale, os.time, os.tmpnameprint(os.date())--example 37 --扩展库或者外部库--Lua支持外部模块要用到require函数--INFO:将弹出对话,但是它可以隐藏在控制台require( "iuplua" )ml = iup.multiline { expand="YES", value="Quit this multiline edit app to continue Tutorial!", border="YES" }dlg = iup.dialog{ml; title="IupMultiline", size="QUARTERxQUARTER",}dlg:show()print("Exit GUI app to continue!")iup.MainLoop()--example 38 --[[--学习更多关于Lua:---Lua Tutorials: http://lua-users.org/wiki/TutorialDirectory --"Programming in Lua" Book: http://www.inf.puc-rio.br/~roberto/pil2/ -- Lua 5.1 Reference Manual: -- Start/Programs/Lua/Documentation/Lua 5.1 Reference Manual --Examples: Start/Programs/Lua/Examples--]]--
0 0
原创粉丝点击