LUA 触动精灵读取文本指定行,获取总行数,等读取函数待更新

原创,自封装函数
小白文,注释很多,大神勿喷。

--读取指定行(注意文本不要存在超过两个空行,没做判断,不要超过总函数)
--参数1路径,参数2行数
--返回字符串
--注:以下函数调用了自封装的xgetseed() 和Xsjs() 函数
function MYreadline(path,hang)
    
    local file = io.open(path,"r");
    if file == nil then--为什么我不在下面的if直接用else,因为我在输出的时候多输出一个\r
        nLog("文件不存在,停止")
        lua_exit()
        mSleep(10)
    end 
    
    --local mhang = 0
    if file then
        local newlist = {};
        for eveline in file:lines() do
            table.insert(newlist,eveline)
        end
        file:close();
        --mhang = #newlist)--获取总函数
        return string.gsub(newlist[hang],"\r","")
    end 
    
end

--随机读取行(注意文本不要存在超过两个空行,懒得在写了,待以后完善,两个空行的时候,随机读取可能会读取到nil)
--参数1路径
--返回字符串
--注:以下函数调用了自封装的xgetseed() 和Xsjs() 函数
function MYranline(path)
    
    local file = io.open(path,"r");
    if file == nil then
        nLog("文件不存在,停止")
        lua_exit()
        mSleep(10)
    end 
    
    if file then
        local newlist = {};
        for eveline in file:lines() do
            table.insert(newlist,eveline)
        end
        file:close();
        return string.gsub(newlist[Xsjs(1, (#newlist))],"\r","")
    end
    
end


--设置随机因子
--需要加载函数local socket = require("szocket")
function xgetseed() 
    local t = string.format("%f", socket.gettime())
    local st = string.sub(t, string.find(t, "%.") + 1, -1) 
    return tonumber(string.reverse(st))
end 

--随机数
function Xsjs(sja, sjb) 
    math.randomseed(xgetseed()) 
    return math.random(sja, sjb)
end 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。