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,b,c,d)
1         2        c       table: 0041BC58
>  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.
> a=a.."看我给接上"  --字符串用两个..可以进行连接
>   print (a)
single 
' quoted '  string  and  double  " quoted "  string inside 看我给接上

我们关注一下lua比较特别的地方

  • 声明变量及给变量赋值的时候,可以多变量一起赋值
  • 字符串常量可以用双引号及单引号,并且混用的时候无须转义字符,跟php有点类型,还可以用"[[]]"括起来,
    这一点类似于c#里面字符串可以加上@符号,可以保留字符串内的格式

 4)逻辑控制语句

>  a = 10   -- 逻辑控制语句就不费口舌了 ,反正就这几个词,跟pl/sql有点像
>   if  a == 10   then
>>   print ( " 我是10 " )
>>   elseif  a ==   11   then
>>   print ( " 我是11 " )
>>   else
>>   print ( " 我是水户洋平等等 " )
>>   end
我是10

5)循环结构

ContractedBlock.gif ExpandedBlockStart.gif 循环结构
> --第一种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
>   -- 3.甚至可以返回多个变量
>  a = 1
>  b = 2
>   function  myFunc3(A,B)
>>        return  B,A
>>   end
>  c  =  myFunc3(a,b)  -- c会等于返回的第一个,在此倒转过来即为2
>   print (c)
2
>  c,d = myFunc3(a,b)  -- 当然如果给多个变量赋值就没有问题了
>   print (c,d)
2         1
>   print (myFunc3(a,b))  -- 直接打印就把所有打印出来
2         1
>   -- 4.还可以是变参
>   function  myFunc4()
>>   print ()
>>   end
>  myFunc4(a,b,c,d)
1         2         2         1

 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 , 2 , 3 , " a " , ' b ' ,linbc}
>   for  i,v  in   pairs (arr)  do
>>      print (i,v)
>>   end
1         1
2         2
3         3
4        a
5        b
6        table: 0041ED50
>    -- 既然作为数组吧,那肯定是可以增加记录的吧
>   table.insert (arr,A)
>  table.foreach(arr, function (i,v)  print (v)  end )
1
2
3
a
b
table: 0041ED50
table: 0041ED50
>   -- 瞧上面,可以foreach,还可以用匿名函数喔
>   -- 当然也可以用下标访问了
>   print (arr[ 1 ])
1
>   -- 调用table.insert会自动给增加下标,如
>   table.insert (linbc, " hello " )
>   print (arr[ 1 ])
1

 

基本上lua是一门比较简洁的语言,感觉跟javascript like 80%,很轻量级

 

转载于:https://www.cnblogs.com/linbc/archive/2009/06/02/1494622.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值