PtokaX forum

Archive => Archived 5.1 boards => Help with scripts => Topic started by: sphinx_spb on 08 November, 2007, 20:09:52

Title: read lines
Post by: sphinx_spb on 08 November, 2007, 20:09:52
Help me with this code...
I want to read lines from log file, but apply filter to them.
I need not all lines, but only lines that contain given nick.
Logfile format is:
[2007_11_08 05:59] <user1>(10.27.42.206) hello
[2007_11_08 05:59] <user2>(10.179.99.250) hi

The command '+mlog 2007_11_08' works fine and read all lines.
And '+mlog 2007_11_08 user1' isn't work - again read all lines.
I think the error is in function loadtext, marked by --[[   ]]  but I don't know how to fix.

The code follows:

function getFullLogs(sUser,arg)
local _,_,f, fdate,fnick = string.find(arg, "%b<>%s+%p(%S+)%s+(%S+)%s+(%S+)");
msg=""
logtype = F_MainLogPath..fdate.."_mainchat.log"
msg = msg..loadtext(logtype,fnick).."\r\n"
BotSayToUser(sUser,msg )
return 1
end

function loadtext(filename,nick)
local message = ""
local list = {}
local f = io.open( filename, "r" )
if f then
message=f:read("*all")
--[[
if nick ~= nil then
for line in f:lines() do
if string.find(line,nick) then table.insert(list,line); end
end
message=list
end
]]
f:close()
end
return message
end
Title: Re: read lines
Post by: CrazyGuy on 09 November, 2007, 00:38:16
try this


function loadtext(filename,nick)
local list = {}
local f = io.open( filename, "r" )
if f and nick then
for line in f:lines() do
if string.find(line,nick) then table.insert(list,line) end
end
f:close()
end
return list
end
Title: Re: read lines
Post by: sphinx_spb on 09 November, 2007, 18:11:22
Big thanks CG and Mutor, works fine! 8)
Title: Re: read lines
Post by: bastya_elvtars on 09 November, 2007, 20:12:05
If it's 500% sure that the file exists, you ought to take io.lines() into consideration.
Title: Re: read lines
Post by: CrazyGuy on 10 November, 2007, 15:21:42
that code looks familiar, I wonder why...  :P