PtokaX forum

Archive => Archived 4.0 boards => Request for Lua 4 scripts => Topic started by: ruler on 04 June, 2004, 15:55:36

Title: Help*** removing lines?
Post by: ruler on 04 June, 2004, 15:55:36
howdy
does anyone have a simple way of removing a line from a text file?
the type of file i want to use will hold all the nicks of all unregistered users in the hub.
as the user leaves the hub, the script hopfully will remove that nick from the list in the text file and as they enter it will add the nick.

thanks
Title:
Post by: nErBoS on 05 June, 2004, 00:24:08
Hi,

Try this one...

--Requested by ruler
--Made by nErBoS

sBot = "User Logger"

arrUserLog ={}
fUserLog = "userlog.dat"

function Main()
frmHub:RegBot(sBot)
LoadFromFile(fUserLog)
end

function OnExit()
SaveToFile(fUserLog , arrUserLog , "arrUserLog")
end

function NewUserConnected(user)
arrUserLog[user.sName] = "logged"
end

OpConnected = NewUserConnected

function UserDisconnected(user)
arrUserLog[user.sName] = nil
end

OpDisconnected = UserDisconnected

function Serialize(tTable, sTableName, sTab)
assert(tTable, "tTable equals nil");
assert(sTableName, "sTableName equals nil");

assert(type(tTable) == "table", "tTable must be a table!");
assert(type(sTableName) == "string", "sTableName must be a string!");

sTab = sTab or "";
sTmp = ""

sTmp = sTmp..sTab..sTableName.." = {\n"

for key, value in tTable do
local sKey = (type(key) == "string") and format("[%q]",key) or format("[%d]",key);

if(type(value) == "table") then
Serialize(value, sKey, sTab.."\t");
else
local sValue = (type(value) == "string") and format("%q",value) or tostring(value);
sTmp = sTmp..sTab.."\t"..sKey.." = "..sValue
end

sTmp = sTmp..",\n"
end

sTmp = sTmp..sTab.."}"
return sTmp
end

function SaveToFile(file , table , tablename)
writeto(file)
write(Serialize(table, tablename))
writeto()
end

function LoadFromFile(file)
if (readfrom(file) ~= nil) then
readfrom(file)
dostring(read("*all"))
readfrom()
end
end

Best regards, nErBoS
Title:
Post by: ruler on 06 June, 2004, 10:51:30
thanks nErBoS
just what i was looking for :)
i spent hours searching the forum but couldnt find anything.
thanks again

regards
Title:
Post by: nErBoS on 06 June, 2004, 13:23:12
Hi,

Made a little update to clean the list if the HUB is shutdown...

--Requested by ruler
--Made by nErBoS

sBot = "User Logger"

arrUserLog ={}
fUserLog = "userlog.dat"

function Main()
frmHub:RegBot(sBot)
LoadFromFile(fUserLog)
CheckTable()
end

function OnExit()
SaveToFile(fUserLog , arrUserLog , "arrUserLog")
end

function NewUserConnected(user)
arrUserLog[user.sName] = "logged"
end

OpConnected = NewUserConnected

function UserDisconnected(user)
arrUserLog[user.sName] = nil
end

OpDisconnected = UserDisconnected

function CheckTable()
local usr,aux
for usr, aux in arrUserLog do
if (GetItemByName(usr) == nil) then
arrUserLog[usr] = nil
end
end
end

function Serialize(tTable, sTableName, sTab)
assert(tTable, "tTable equals nil");
assert(sTableName, "sTableName equals nil");

assert(type(tTable) == "table", "tTable must be a table!");
assert(type(sTableName) == "string", "sTableName must be a string!");

sTab = sTab or "";
sTmp = ""

sTmp = sTmp..sTab..sTableName.." = {\n"

for key, value in tTable do
local sKey = (type(key) == "string") and format("[%q]",key) or format("[%d]",key);

if(type(value) == "table") then
Serialize(value, sKey, sTab.."\t");
else
local sValue = (type(value) == "string") and format("%q",value) or tostring(value);
sTmp = sTmp..sTab.."\t"..sKey.." = "..sValue
end

sTmp = sTmp..",\n"
end

sTmp = sTmp..sTab.."}"
return sTmp
end

function SaveToFile(file , table , tablename)
writeto(file)
write(Serialize(table, tablename))
writeto()
end

function LoadFromFile(file)
if (readfrom(file) ~= nil) then
readfrom(file)
dostring(read("*all"))
readfrom()
end
end

Best regards, nErBoS
Title:
Post by: ruler on 06 June, 2004, 19:42:59
nice peice of code and works just fine  8)
thanks