1. 程式人生 > >一段讀取檔案,逆序排列的lua指令碼

一段讀取檔案,逆序排列的lua指令碼


local method = ngx.var.request_method;
local headers = ngx.req.get_headers();
local uri_args = ngx.req.get_uri_args();
-- 逆序排列函式
function reverseTable(tab)
	local tmp = {}
	for i = 1, #tab do
		local key = #tab
		tmp[i] = table.remove(tab)
	end
	return tmp
end
if method == "GET" then
    ngx.say("version2.0");
    local opt = uri_args["opt"];
    if("r" == opt) then
        file = io.open("/logs/httppost.log", "r");
    file:seek("end", -8000);
    local content = file:read("*l");
    local count = 1;
    array = {};
    while(nil ~= content) do
        --ngx.say(content);
	    array[count]=content;
	    count = count+1;
        content = file:read();
    end
    file:close();
    newArray=reverseTable(array);
    for i=1,#newArray do
        ngx.say(newArray[i]);
	end                   
else
    ngx.say("操作不是讀檔案");
end
end