刚开始自学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") end for 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
详细的更多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 object myTable = {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 statement for k,v in pairs(myTable) do io.write("the key : "..k," the value : "..v,"\n") end for 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 --test callback function myFunc () print("My Name is : Lin Jian Feng, E-Name : Jave.Lin") return 1 end myFunc() local callback=myFunc; callback() function mySpeakHandler() print("i like the LOL Game!") end myTable.speak=mySpeakHandler;--set the callback myTable.speak()--call it --return mutil results function getMutilSpeak() local callback1,callback2,callback3 function tempFunc1(s)--attention : nested func print(s.." i'am jave.lin, and i like 3c")-- ".." can combin the string end function tempFunc2(s) print(s.." i'am jave.lin, and i like dota") end function tempFunc3(s) print(s.." i'am jave.lin, and i like music") end callback1=tempFunc1 callback2=tempFunc2 callback3=tempFunc3 return callback1,callback2,callback3 end local c1,c2,c3=getMutilSpeak() c1("direct call") c2("direct call") c3("direct call") --create func array/table, and calls --method1 funcTable={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 statement function action(idx,tempFunc) tempFunc("from method3, the "..idx.." : ") end table.foreach(funcTable,action) --method4 --use anonymous func table.foreach(funcTable,function(idx,tempFunc) tempFunc("from method4, the "..idx.." : anonymous func : ") end) --test concat strTable={"iOS","Android","Windows","Others"} print("i like os : "..table.concat(strTable,"-"))--print result : i like os : iOS-Android-Windows-Others strTable={"H","A","P","P","Y"} print("are you "..table.concat(strTable))--are you HAPPY --test sort strTable={"c","b","a"} print("src strTable values : "..table.concat(strTable," "))--src strTable values : c b a table.sort(strTable) print("and now sorted values : "..table.concat(strTable," "))--and now sorted values : a b c --test insert table.insert(strTable,3,"d") print("inserted values : "..table.concat(strTable," "))--inserted values : a b d c table.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 remove table.remove(strTable,1)--remove first element print("removed values : "..table.concat(strTable," "))--removed values : c b a --test maxn --result max key's(not value), and key must be a number print("strTable.maxn : "..table.maxn(strTable)) t={["abceeee"]=0,["abcd"]=1,["abcde"]=3}--no key is number element, so maxn == 0 print("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 == 3 print("t.maxn : "..table.maxn(t)) --test getn print("t element count : "..table.getn(t)..table.concat(t,", "))--t element count : 3 table.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.lin ready to test the table statement the key : 1 the value : 1 the key : 2 the value : 2 the key : 3 the value : 3 the key : 4 the value : the 4 idx value the key : firstname the value : jave the key : favorate the value : game the key : lastname the value : lin the key : old the value : 26 myTable[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.Lin My Name is : Lin Jian Feng, E-Name : Jave.Lin i like the LOL Game! direct call i'am jave.lin, and i like 3c direct call i'am jave.lin, and i like dota direct call i'am jave.lin, and i like music from method1 i'am jave.lin, and i like 3c from method1 i'am jave.lin, and i like dota from method1 i'am jave.lin, and i like music from method2 i'am jave.lin, and i like 3c from method2 i'am jave.lin, and i like dota from method2 i'am jave.lin, and i like music from method3, the 1 : i'am jave.lin, and i like 3c from method3, the 2 : i'am jave.lin, and i like dota from method3, the 3 : i'am jave.lin, and i like music from method4, the 1 : anonymous func : i'am jave.lin, and i like 3c from method4, the 2 : anonymous func : i'am jave.lin, and i like dota from method4, the 3 : anonymous func : i'am jave.lin, and i like music i like os : iOS-Android-Windows-Others are you HAPPY src strTable values : c b a and now sorted values : a b c inserted values : a b d c and now desc sorted values : d c b a removed values : c b a strTable.maxn : 3 t.maxn : 0 t.maxn : 3 t element count : 311, 22, 33 t element count : 222, 33 >Exit code: 0