Lua Self-study Log

来源:互联网 发布:网络拓扑是指 编辑:程序博客网 时间:2024/05/22 18:24

刚开始自学Lua;

感觉脚本语言都是一类,语法不严密,所以规则少了,学起来,就更加容易了;

首先,我是使用Windows系统,那么就得下个,Windows下Lua专用的Editor了;

LuaForWindow_5.1

简称:Lfw


安装完后;

运行:Intruduction程序;(安装完后,有提示运行的);

当然如果没有留意到上面操作的,也可以自己手动运行,

Lua安装目录/5.1/examples/quickluatour.lua;也一样可以让Intruduction程序再次运行;


好了,看完quickluatour.lua后,如果都了解了,那么恭喜你,你已入门喇;哈哈;

当然,你也可以,打开,Lfw的帮助手册(.chm)来瞅瞅;在安装目录下可以找到;

如果还没找到或是实在不想找,你也可以运行Lfw后,再按下:F1后,会自动打开"Lua 5.1.4.chm"的;

当然,除非说,这个文件真不存在,那就悲催了;


瞅瞅我第一个简单的DEMO:

io.write("hello world! my name is jave.lin\n");io.write("ready to test the table statement\n");myTable = {1,2,3,"the 4 idx value"};myTable.old=26;myTable.firstname="jave";myTable.lastname="lin";myTable.favorate="game";for k,v in pairs(myTable) do io.write("the key : "..k," the value : "..v,"\n") endfor k,v in pairs(myTable) do io.write(string.format("myTable[%s]=%s,k is [%s], v is [%s]\n",tostring(k),tostring(v),tostring(type(k)),tostring(type(v))))end

瞅瞅Lfw运行效果:



详细的更多DEMO:

--test hello wrod!io.write("hello world! my name is jave.lin\n");io.write("ready to test the table statement\n");--test table objectmyTable = {1,2,3,"the 4 idx value"};myTable.old=26;myTable.firstname="jave";myTable.lastname="lin";myTable.favorate="game";--test table conver to paris by for statementfor k,v in pairs(myTable) do io.write("the key : "..k," the value : "..v,"\n") endfor k,v in pairs(myTable) doio.write(string.format("myTable[%s]=%s,k is [%s], v is [%s]\n",tostring(k),tostring(v),tostring(type(k)),tostring(type(v))))end--test callbackfunction myFunc ()print("My Name is : Lin Jian Feng, E-Name : Jave.Lin")return 1endmyFunc()local callback=myFunc;callback()function mySpeakHandler() print("i like the LOL Game!") endmyTable.speak=mySpeakHandler;--set the callbackmyTable.speak()--call it--return mutil resultsfunction getMutilSpeak()local callback1,callback2,callback3function tempFunc1(s)--attention : nested funcprint(s.." i'am jave.lin, and i like 3c")-- ".." can combin the stringendfunction tempFunc2(s)print(s.." i'am jave.lin, and i like dota")endfunction tempFunc3(s)print(s.." i'am jave.lin, and i like music")endcallback1=tempFunc1callback2=tempFunc2callback3=tempFunc3return callback1,callback2,callback3endlocal c1,c2,c3=getMutilSpeak()c1("direct call")c2("direct call")c3("direct call")--create func array/table, and calls--method1funcTable={c1,c2,c3}for i=1,3 do funcTable[i]("from method1") end--method2--attention table conver to pairs kword: pairs(tableObj)for k,v in pairs(funcTable) do v("from method2") end--method3--use foreach statementfunction action(idx,tempFunc)tempFunc("from method3, the "..idx.." : ")endtable.foreach(funcTable,action)--method4--use anonymous functable.foreach(funcTable,function(idx,tempFunc) tempFunc("from method4, the "..idx.." : anonymous func : ") end)--test concatstrTable={"iOS","Android","Windows","Others"}print("i like os : "..table.concat(strTable,"-"))--print result : i like os : iOS-Android-Windows-OthersstrTable={"H","A","P","P","Y"}print("are you "..table.concat(strTable))--are you HAPPY--test sortstrTable={"c","b","a"}print("src strTable values : "..table.concat(strTable," "))--src strTable values : c b atable.sort(strTable)print("and now sorted values : "..table.concat(strTable," "))--and now sorted values : a b c--test inserttable.insert(strTable,3,"d")print("inserted values : "..table.concat(strTable," "))--inserted values : a b d ctable.sort(strTable,function(v1,v2) return v1>v2 end)print("and now desc sorted values : "..table.concat(strTable," "))--and now desc sorted values : d c b a--test removetable.remove(strTable,1)--remove first elementprint("removed values : "..table.concat(strTable," "))--removed values : c b a--test maxn--result max key's(not value), and key must be a numberprint("strTable.maxn : "..table.maxn(strTable))t={["abceeee"]=0,["abcd"]=1,["abcde"]=3}--no key is number element, so maxn == 0print("t.maxn : "..table.maxn(t))t={11,22,33,["abceeee"]=0,["abcd"]=1,["abcde"]=3}--no key ,default number, 11,22,33,keys is : 1,2,3,three number,but ["abceeee"],["abcd"],["abcde"] is Nan, so maxn == 3print("t.maxn : "..table.maxn(t))--test getnprint("t element count : "..table.getn(t)..table.concat(t,", "))--t element count : 3table.remove(t,1);print("t element count : "..table.getn(t)..table.concat(t,", "))--t element count : 2--test setn--table.setn(t,10);--it's obsolete(作废的) in 5.1 version--以上的方法中,很多方法都是使用了table下的方法,可以参考:http://blog.163.com/yunfei_lei@126/blog/static/14086456120128848799/--的介绍

运行结果:

>lua -e "io.stdout:setvbuf 'no'" "myFirstLua.lua" hello world! my name is jave.linready to test the table statementthe key : 1 the value : 1the key : 2 the value : 2the key : 3 the value : 3the key : 4 the value : the 4 idx valuethe key : firstname the value : javethe key : favorate the value : gamethe key : lastname the value : linthe key : old the value : 26myTable[1]=1,k is [number], v is [number]myTable[2]=2,k is [number], v is [number]myTable[3]=3,k is [number], v is [number]myTable[4]=the 4 idx value,k is [number], v is [string]myTable[firstname]=jave,k is [string], v is [string]myTable[favorate]=game,k is [string], v is [string]myTable[lastname]=lin,k is [string], v is [string]myTable[old]=26,k is [string], v is [number]My Name is : Lin Jian Feng, E-Name : Jave.LinMy Name is : Lin Jian Feng, E-Name : Jave.Lini like the LOL Game!direct call i'am jave.lin, and i like 3cdirect call i'am jave.lin, and i like dotadirect call i'am jave.lin, and i like musicfrom method1 i'am jave.lin, and i like 3cfrom method1 i'am jave.lin, and i like dotafrom method1 i'am jave.lin, and i like musicfrom method2 i'am jave.lin, and i like 3cfrom method2 i'am jave.lin, and i like dotafrom method2 i'am jave.lin, and i like musicfrom method3, the 1 :  i'am jave.lin, and i like 3cfrom method3, the 2 :  i'am jave.lin, and i like dotafrom method3, the 3 :  i'am jave.lin, and i like musicfrom method4, the 1 : anonymous func :  i'am jave.lin, and i like 3cfrom method4, the 2 : anonymous func :  i'am jave.lin, and i like dotafrom method4, the 3 : anonymous func :  i'am jave.lin, and i like musici like os : iOS-Android-Windows-Othersare you HAPPYsrc strTable values : c b aand now sorted values : a b cinserted values : a b d cand now desc sorted values : d c b aremoved values : c b astrTable.maxn : 3t.maxn : 0t.maxn : 3t element count : 311, 22, 33t element count : 222, 33>Exit code: 0


原创粉丝点击