zoukankan      html  css  js  c++  java
  • Lua 基础学习笔记

    执行Lua脚本

    lua hello.lua
    

    运行完指定脚本后进入交互式程序

    lua -i hello.lua
    

    在解释器中加载指定脚本

    dofile("hello.lua")
    

    直接在命令行运行代码

    lua -e "print(math.sin(12))"
    

    加载脚本

    lua -l hello.lua
    

    -- 行注释
    --[[ 块注释 ]]--

    如果要删除一个全局变量,则a=nil即可。

    长字符串
    page =[[
    这是一个长字符串
    ]]

    .. 是字符串的连接符,必须使用空格分割开
    # 在字符串前面,表示获取字符串的长度
    tonumber() 将string转为number
    tostring() 将number转为string

    Lua通过table来表示模块,包和对象的。
    table是一种动态分配的对象。
    table通过“构造表达式”创建的,{}
    table永远是“匿名的”,一个持有table的变量和table自身之间没有固定的关联性。

    a = {}
    a["name"] = "ifan"
    print(a["name"])
    -- 打印所有行
    for i=1, #a do
      print(a[i])
    end
    

    所有未初始化的元素索引结果都是nil。Lua将nil作为界定数组结尾的标志。

    a={}
    a[1000]=1000
    print(#a)
    > 0
    print(table.maxn(a))
    > 1000
    

    初始化table

    list = {1,2,3,4,5}
    dict = {"a"=1, "b"=2, "c"=3}
    
    if a < 0 then
    a = 0
    end
    --
    if a < 0 then 
      a = 0
    else 
      a = 1
    end
    --
    if a > 1 then
      a = 1
    elseif a > 0 then
      a = 1
    else:
      a = 0 
    end
    
    local i = 1
    while a[i] do
      print(a[i])
      i = i + 1
    end
    
    --
    -- 至少执行一次的while循环
    repeat
      line = io.read()
    until line ~= ""
    print(line)
    
    for 开始值, 结束值, 步长(默认为1) do
      
    end
    
    for i=0, 10, 2 do
      print(i)
    end
    
    -- 泛型遍历
    for k,v in pairs(a) do 
      print(k, v)
    end
    -- pairs 遍历table
    -- ipairs 遍历数组
    -- string.gmatch 遍历字符串中单词
    

    unpack 接受数组,返回每个元素

    -- 接收可变参数
    function add (...)
      local s = 0
      for i,v in ipairs{...} do
        s = s + v
      end
      return s
    end
    

    对table进行排序

    network = {
      {name="n1", ip="192.168.1.1"},
      {name="n2", ip="192.168.1.2"},
      {name="n3", ip="192.168.1.3"},
      {name="n4", ip="192.168.1.4"},
    }
    table.sort(network, function(a,b) return (a.name > b.name) end)
    

    尾调用消除,在尾调用之后,程序不需要保存任何关于该函数的栈信息。
    在Lua中,只有return <func>(<args>)这样的调用才算一条“尾调用”。
    状态机

    实现迭代器

    function values(t)
      local i = 0
      return function () i = i + 1; return t[i] end
    end
    

    使用迭代器

    t = {1, 2, 3}
    iter = values(t)
    while true do
      local element = iter()
      if element == nil then break end
      print(element)
    end
    
    for element in values(t) do
      print(element)
    end
    

    执行字符串代码

    -- 执行错误 返回nil
    f = loadstring("local i = 0; i = i + 1")
    f(); print(i)
    f(); print(i)
    

    在Lua中,函数定义是一种赋值操作,只有在运行时才完成的操作。

    处理异常

    if pcall(function() 
        <受保护的代码>
      end) then
        <常规代码>
    else
        <错误处理代码>
    end
    

    如果没有一场,pcall会返回true和函数调用的返回值;否则返回false及错误消息。

    一个协同程序可以处于4中不同的状态:挂起,运行,死亡,正常。
    当创建一个协同程序时,处于挂起状态。

    co = coroutine.create(function() print("hi") end)
    print(co)
    -- 检查程序的状态
    print(coroution.status(co))
    -- 运行协同程序
    coroution.resume(co)
    print(coroution.status(co))
    
    -- 
    co = coroutine.create(function (start, end)
      for i=tonumber(start), tonumber(end) do
        print("co ", i)
        coroutine.yield()
      end
      end)
    -- 传递参数
    coroutine.resume(co, 1, 10)
    

    resume是在保护模式中运行的,在一个协同程序的执行中发生任何错误,都不会显示错误消息,而是将执行权返回给resume调用。

    -- 实现管道和过滤器
    function receive(prod)
      local status, value = coroutine.resume(prod)
      return value
    end
    
    function send(x)
      coroutine.yield(x)
    end
    
    function producer()
      return coroutine.create(function ()
        for i=1, 10 do
          print("producer = ", i)
          send(i)
        end
      end)
    end
    
    function filter(prod)
      return coroutine.create(function() 
        while true do
         local x = receive(prod)
         print("filter = ", x)
         send(x)
        end
      end)
    end
    
    function consumer(prod)
       while true do
        local x = receive(prod)
        if x == nil then break end
        print("consumer = ", x)
      end
    end
    
    consumer(filter(producer()))
    
  • 相关阅读:
    .Net魔法堂:log4net详解
    CentOS6.5菜鸟之旅:安装SUN JDK1.7和Tomcat7
    Java魔法堂:注释和注释模板
    Eclipse魔法堂:任务管理器
    CentOS6.5菜鸟之旅:VirtualBox4.3识别USB设备
    Windows魔法堂:解决“由于启动计算机时出现页面文件配置问题.......”
    JS魔法堂:IE5~9的Drag&Drop API
    CentOS6.5菜鸟之旅:安装VirtualBox4.3
    HTML5魔法堂:全面理解Drag & Drop API
    byzx
  • 原文地址:https://www.cnblogs.com/iFanLiwei/p/13185354.html
Copyright © 2011-2022 走看看