lua脚本语言快速入门教程
lua作为很好的嵌入式语言可以非常好的作为c/c++补充,在游戏行业是得到了广泛的应用 一直在思考,能不能把他引入商业领域的规则语言呢?将业务规则经常变的部分提炼出来, 而无须重新编译程序。作为规则引擎的角色进行使用 使用前当然得安装一下去http://www.lua.org/下载一个 Lua_v5.1.4.23.exe安装,装完之后执行 可以用 lua.exe逐行解释的方式,或者写完脚本用lExecutor.wlua执行 1)先来个helloworld 复制代码 代码如下: > print 'helloWorld' helloWorld > print("helloWorld") --注释是这么写的! helloWorld > a='hello' > print(a) hello 2)主要类型 复制代码 代码如下: > a=1 > b="abc" > c={} > d=print > > print(type(a)) number > print(type(b)) string > print(type(c)) table > print(type(d)) function > 如上所示,lua主要有四种类型,即数字,字符串,table(其实理解可以理解为javascript的object),以及函数类型, 当然了,还有最常的bool型了,true以及false,(这里不考虑nil,以及userdata,thread等) 函数类型的变量,加上”() “即可以执行,如 复制代码 代码如下: > d(b) abc 3)变量及常量,字符串 复制代码 代码如下: > a,b,c,d = 1,2,'c',{1} > print (a,d) > a="single 'quoted' string and double "quoted" string inside" > b='single 'quoted' string and double "quoted" string inside' > c= [[ multiple line >> with'single' >> and "double" quoted strings inside.]] > > print(a) single 'quoted' string and double "quoted" string inside > print(b) single 'quoted' string and double "quoted" string inside > print(c) multiple line with'single' and "double" quoted strings inside.
> print(a) single 'quoted' string and double "quoted" string inside看我给接上 我们关注一下lua比较特别的地方 1.声明变量及给变量赋值的时候,可以多变量一起赋值 这一点类似于c#里面字符串可以加上@符号,可以保留字符串内的格式 4)逻辑控制语句 复制代码 代码如下: > a=10 --逻辑控制语句就不费口舌了,反正就这几个词,跟pl/sql有点像 > if a==10 then >> print("我是10") >> elseif a== 11 then >> print("我是11") >> else >> print("我是水户洋平等等") >> end 我是10 5)循环结构 复制代码 代码如下: > --第一种while循环 > a=1 > while a~=10 do >> io.write(a.." ") >> a=a+1 >> end 1 2 3 4 5 6 7 8 9 > > --第二种repeat、until循环 > a=0 > repeat >> a=a+1 >> print(a) >> until a==5 1 2 3 4 5 > --第三种for循环 > for a=1,4 do >> io.write(a) >> end 1234> > --比较特别,有个步进的概念,如下是以2为步进 13> for a=1,4,2 do >> io.write(a..' ') >> end 1 3 > for a=1,5,2 do >> io.write(a.. ' ') >> end 1 3 5 > 6)函数及其使用 复制代码 代码如下: > --1.先来个最简单的 > function myFunc1() >> print("hello function") >> end > myFunc1() hello function > --2.再来个带参数的 > a=1 > b=2 > function myFunc2(par1,par2) >> return par1+par2 >> end > c=myFunc2(a,b) > print(c) > --3.甚至可以返回多个变量 > a=1 > b=2 > function myFunc3(A,B) >> return B,A >> end > c = myFunc3(a,b) --c会等于返回的第一个,在此倒转过来即为2 > print(c) > c,d=myFunc3(a,b) --当然如果给多个变量赋值就没有问题了 > print(c,d) > print(myFunc3(a,b)) --直接打印就把所有打印出来 > --4.还可以是变参 > function myFunc4() >> print() >> end > myFunc4(a,d) 7)table(我觉得理解object也可以)的使用 复制代码 代码如下: > linbc={} > linbc.sex = "男" > linbc.age = 27 > print(linbc,linbc.sex,linbc["age"]) table: 0041ED50 男 27 > A=linbc > print(A["sex"]) 男 > print(A) --引用类型,看A以及linbc内存地址是一样的 table: 0041ED50 > --瞧这用法,跟javascript的用法基本相当 > --其实还可以理解为hashmap似的键值对 > for i,v in pairs(linbc) do >> print(i,v) >> end age 27 sex 男 > --也可以作为数组,下标是从1开始的喔 > arr={1,3,"a",'b',linbc} > for i,v in pairs(arr) do >> print(i,v) >> end > --既然作为数组吧,那肯定是可以增加记录的吧 > table.insert(arr,A) > table.foreach(arr,function(i,v) print(v) end) a b table: 0041ED50 table: 0041ED50 > --瞧上面,可以foreach,还可以用匿名函数喔 > --当然也可以用下标访问了 > print(arr[1]) > --调用table.insert会自动给增加下标,如 > table.insert(linbc,"hello") > print(arr[1]) 1 您可能感兴趣的文章:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |