lua分析文件

发布于:2025-07-18 ⋅ 阅读:(21) ⋅ 点赞:(0)


    function startwith(str, substr)  
        if str == nil or substr == nil then  
            return nil, "the string or the sub-stirng parameter is nil"  
        end  
        if string.find(str, substr) ~= 1 then  
            return false  
        else  
            return true  
        end  
    end

    function endwith( str, substr)  
        if str == nil or substr == nil then  
            return nil, "the string or the sub-string parameter is nil"  
        end  
        str_tmp = string.reverse(str)  
        substr_tmp = string.reverse(substr)  
        if string.find(str_tmp, substr_tmp) ~= 1 then  
            return false  
        else  
            return true  
        end  
    end

function split(str, sep)
    local result = {}
    if str == nil or sep == nil or type(str) ~= "string" or type(sep) ~= "string" then
        return result
    end
 
    if string.len(sep) == 0 then
        return result
    end
    local pattern = string.format("([^%s]+)", sep)
    --print(pattern)
 
    string.gsub(
        str,
        pattern,
        function(c)
            result[#result + 1] = c
        end
    )
 
    return result
end


local fileName = "d:\\1.001"
print (fileName)
local file = io.open(fileName, "r")
local kfLines = {}
local lineIndex= 1
for line in file:lines() do
    kfLines[lineIndex] = line
    lineIndex = lineIndex+1
end

local pStartLine = 0
local pEndLine = 0
for i, line in ipairs(kfLines) do
    --print(i, line)
    if startwith(line, "xxxList") then
        pStartLine = i + 1
    end
    if startwith(line, "xxxEnd") then
        pEndLine = i-1
    end
end

print("p info list:")
local x_y_table={}
for i = pStartLine, pEndLine do
    local arr = split(kfLines[i], " ")
    local x_y = arr[2].."_"..arr[3]
    if rawget(x_y_table, x_y) ~= nil then
        x_y_table[x_y] = x_y_table[x_y] + 1
    else
        x_y_table[x_y] = 1
    end
    --print("split result "..arr[1].."  "..x_y)
end

for k, v in pairs(x_y_table) do
    print(k, v)
    
end
print("articleStartLine="..tostring(pStartLine))
print("articleEndLine="..tostring(pEndLine))
return x_y_table


网站公告

今日签到

点亮在社区的每一天
去签到