美文网首页
Lua库函数概览

Lua库函数概览

作者: 小城大麦 | 来源:发表于2016-08-16 17:28 被阅读37次

    Lua库函数概览
    数学库

    math {
        sin,cos,tan,asin,acos,exp,log,log10,floor,ceil,max,min
        random,randomseed,pi
        deg, -- radius->degree
        rad, -- degree->radius
        huge -- lua能表示的最大数字
    }
    -- os.time  返回描述
    math.randomseed(os.time()) -- 设定随机
    

    table库

    table {
        insert(t,index,value)
        sort,
        concat,
    }
    t = {1}
    local len = #t -- 获得table的长度(数组table)
    

    字符串库

    string{
        lower,upper,rep,len
        sub,
        char,byte
        format
        find,match,gsub,gmatch
    }
    -- -1代表字符串最后一个字符,1代表第一个字符
    -- low方法
    table.sort(a,function(a,b) return string.lower(a) < string.lower(b) end)
    -- 创建字符串的bytes
    all_bytes = string.byte(str,1,-1)
    

    IO库

    io.input, io.output
    io.write, io.read
    io.open(file,'rwb') -- r read, w write b binary
    io.read:
        *all -- 读取整个文件
        *line -- 读取下一行
        *number -- 读取一个数字
        <num> -- 读取不超过<num>个字符的字符串
    io.read(0)可永远检查是否读取到结尾,如果有数据读取,返回空串,否则返回nil
    --
    local temp = io.input() -- 保存当前文件
    io.input("newinput") -- 打开一个新的文件
    .... --对文件的一些操作
    io.input():close() -- 关闭当前文件
    io.input(temp) -- 恢复原来的文件
    --buf方式读取文件
    local BUFSIZE = 2^13 -- 8K
    local lines,rest = f:read(BUFSIZE,"*line")
    -- rest包含被BUF断开那部分
    function fsize(file)
        local current = file:seed() -- 获取当前位置
        local size = file:seek("end") -- 获得文件大小
        file:seek("set",current) -- 恢复当前位置
        return size
    end
    

    os库

    os.remove os.rename
    os.time
    os.clock 返回当前CPU时间的秒数
    os.date
    os.exit 终止当前程序
    os.getenv 获得当前系统环境变量
    os.execute 执行系统命令
    os.setlocale
    

    调试库

    debug{
        getinfo(foo)
         -- 获得一个table
         {
            source -- 函数定义的位置
            short_src --source的短版本
            linedefined --函数定义在源代码的开始行号
            lastlinedefined --函数定义在源代码的结束行号
            what --函数类型
            name --函数的一个适当的名字
            namewhat -- 可能是global, local,method,field或""
            nups: 该函数闭包值的数量
            activelines: 一个table,包含该函数有效代码
            func: --函数本身
         }
    }

    相关文章

      网友评论

          本文标题:Lua库函数概览

          本文链接:https://www.haomeiwen.com/subject/gybpsttx.html