zoukankan      html  css  js  c++  java
  • Lua中的常用语句结构以及函数

     1、Lua中的常用语句结构介绍
              
    --if 语句结构,如下实例:
    gTable = {"hello", 10}
    if nil ~= gTable[1] and "hello" == gTable[1] then
      print("gTable[1] is" , gStringTable[1])
    elseif  10 == gTable[2] then
      print("gTable[2] is", gTable[2])
    else 
      print("unkown gTable element")
    end
    --while 和repeat循环语句结构,while先判断条件,如果true才执行代码块(有可能跳过该代码块);repeat则是在最后判断条件,保证代码块至少执行一次。
    gTable = {1,2,3,4,5,6,7,8,9,10}
    index = 1
    while gTable[index] < 10 do 
       print("while gTable[",index,"] is ",gTable[index])
       index = index + 1 -- 注意,Lua不支持index++或者index += 1形式的运算符。
    end
    --[[
    while循环输出结果如下:
    while gTable[    1    ] is     1
    while gTable[    2    ] is     2
    while gTable[    3    ] is     3
    while gTable[    4    ] is     4
    while gTable[    5    ] is     5
    while gTable[    6    ] is     6
    while gTable[    7    ] is     7
    while gTable[    8    ] is     8
    while gTable[    9    ] is     9
    --]]
    
    --上一个循环结束后,index = 10
    repeat
        print("repeat gTable[",index,"] is ",gTable[index])
       index = index - 2
    until index < 1
    --[[
    输出结果如下:
    repeat gTable[    10    ] is     10
    repeat gTable[    8    ] is     8
    repeat gTable[    6    ] is     6
    repeat gTable[    4    ] is     4
    repeat gTable[    2    ] is     2
    --]]
    
    --for循环结构,for循环结构具有三个参数,初始值,结束值,每个循环增加值。
    for index = 1, 5 do --不设置第三个参数的话,默认缺省第三个参数是1,即每个循环 index 增加1
       print("for cycle index =",index)
    end
    --[[
    输出结果为:
    for cycle index =    1
    for cycle index =    2
    for cycle index =    3
    for cycle index =    4
    for cycle index =    5
    --]]
    
    for index = 20 , 0, -5 do --设定第三个参数为-5
     print("for cycle index:",index)
    end
    --[[
    输出结果:
    for cycle index:    20
    for cycle index:    15
    for cycle index:    10
    for cycle index:    5
    for cycle index:    0
    --]]
     
    --break关键字可以使循环强制退出,Lua中没有continue关键字,需要通过其他方式实现continue关键字,比如if-else语句。或者通过网络下载Lua的continue关键字补丁安装来解决该问题
    
    for index = 1, 100, 5 do 
      if index > 10 and index < 25 then  --用if-else语句实现continue关键字的功能
         print("continue!!!!! index=",index)
      else 
        if index > 15 and index < 35 then 
           print("break~~~~~index=",index)
           break
        end
        print("At end index=",index)
      end
    end
    
    --[[
    输出结果如下:
    At end index=    1
    At end index=    6
    continue!!!!! index=    11
    continue!!!!! index=    16
    continue!!!!! index=    21
    break~~~~~index=    26
    --]]
    
    --最后还要提的一点是,Lua中switch语句的缺失,用if-elseif-else语句代替的话,显得非常臃肿,还有其他的一些实现方案。笔者在网上麦子加菲童鞋的博客中找到一种Lua中代替switch语句非常优雅的方案。下面贴出麦子加菲原代码:
    --Switch语句的替代语法(所有替代方案中觉得最好,最简洁,最高效,最能体现Lua特点的一种方案)
    action = {  
      [1] = function (x) print(x) end,  
      [2] = function (x) print( 2 * x ) end,  
      ["nop"] = function (x) print(math.random()) end,  
      ["my name"] = function (x) print("fred") end,  
    }  
     
    while true do  
        key = getChar()  
        x = math.ramdon()  
        action[key](x)  
    end
    
          2、Lua中的函数
         在Lua脚本中, 函数是以function关键字开始,然后是函数名称,参数列表,最后以end关键字表示函数结束 。需要注意的是, 函数中的参数是局部变量,如果参数列表中存在(...)时,Lua内部将创建一个类型为table的局部变量arg,用来保存所有调用时传递的参数以及参数的个数(arg.n) 。
        
    function PrintTable (pTable)
      for index = 1, table.getn(pTable) do 
          print("pTable[",index,"] =",pTable[index])
      end
    end
    
    gStringTable = {"hello","how","are","you"}
    PrintTable(gStringTable)
    --[[
    输出结果为:
    pTable[    1    ] =    hello
    pTable[    2    ] =    how
    pTable[    3    ] =    are
    pTable[    4    ] =    you
    --]]
    
    function PrintFriendInfo (name, gender, ...) 
      local friendInfoString = string.format("name:%s  gender:%d",name,gender)
      if 0 < arg.n then
         for index = 1, arg.n do 
            friendInfoString = string.format("%s otherInfo:%s",friendInfoString, arg[index])
         end
       end
       print(friendInfoString)
    end
    
    
    PrintFriendInfo ("eric", 1, "程序员","2b", 50)
    
     --输出结果为:
    -- name:eric  gender:1 otherInfo:程序员 otherInfo:2b otherInfo:50
         Lua函数的返回值跟其他语言比较的话,特殊的是能够返回多个返回值。return之后,该Lua函数从Lua的堆栈里被清理。
    function GetUserInfo ()
        local name = "eric"
        local gender = 1
        local hobby = "动漫"
      return name, gender, hobby
    end
    
    print(GetUserInfo())
    
    --输出结果:eric    1    动漫

    三、Lua中的库函数

              在本文的最后,介绍一些Lua中常用的库函数。
              1.数学库
               math库的常用函数: 三角函数math.sin、math.cos、取整函数math.floor、math.ceil、math.max、math.min、随机函数math.random、math.randomseed(os.time())、变量pi和huge。
              2、I/O库
               进行I/O操作前,必须先用io.open()函数打开一个文件。io.open()函数存在两个参数,一个是要打开的文件名,另一个是模式字符,类似"r"表示读取、“w”表示写入并同时删除文件原来内容,“a”表示追加,“b”表示打开二进制文件。该函数会返回一个表示文件的返回值,如果打开出错则返回nil,写入之前需要判断是否出错,比如: local file = assert(io.open(filename, “w”))..使用完毕后,调用io.close(file).或file:close()。
              几个常用I/O函数: io.input ()、io.output ()、 io.read()、 io.write() 。
    local file = assert(io.open(filename, “w”))
    if file ~= nil then
      file:write("hello lua!!!!")  --注意,等同于io.write("hello lua!!!!")
      file:close()  --等同于io.close(file)
    end

             3、调试库

             debug.getinfo()函数,他的第一个参数 可以是一个函数或一个栈层。返回结果是一个table,其中包含了函数的定义位置、行号、函数类型、函数名称等信息。

             debug.getlocal()函数检查函数任意局部变量,有两个参数,第一个是希望查询的函数栈层,另一个是变量的索引。

             assert(trunk)() 函数,执行参数中代码块并在出错时提供报错功能。

    a = "hello world"
    b = "print(a)"
    assert(loadstring(b))()
    --输出结果:
    hello world

            4 、几个 处理 Lua 代 码块的函数

             loadstring(pString)() 函数可以直接 执行 pString 字符串 组成的 Lua 代 码,但不提供报错功能。

    loadstring("for index = 1, 4 do print("for cycle index =",index) end")()
    --[[
    输出结果
    for cycle index =    1
    for cycle index =    2
    for cycle index =    3
    for cycle index =    4
    --]]

              dofile(filename) 函数的功能是 载入并立刻执行 Lua 脚本文件。可以用来 载入定义函数的文件或者数据文件、或立即执行的 Lua 代 码。 dofile 函数会将程序的 执行目录作为当前目录。如果要载入程序执行目录的子目录里的文件,需要加上子目录的路径。

             

    dofile("/Users/ericli/WorkSpace/Lua语言/hellolua.lua")
    --输出结果:Hello Lua!

     

  • 相关阅读:
    vsftpd配置再次冲击Ubuntu之server篇
    update关联其他表批量更新数据
    丁丁的成长7
    Winform中使用PictureBox显示及修改数据库中的照片
    Apache HTTP Server 与 Tomcat 的三种连接方式
    丁丁的成长5
    tomcat的自动启动再次冲击Ubuntu之server篇
    再严重的感冒,马上就好【转】
    基本配置2被忽悠进了CentOS 6
    丁丁的成长6
  • 原文地址:https://www.cnblogs.com/wangxusummer/p/4287889.html
Copyright © 2011-2022 走看看