Hi,
Here is the code for loading and saving tables converted to LUA 5.0...
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 string.format("[%q]",key) or string.format("[%d]",key);
if(type(value) == "table") then
sTmp = sTmp..Serialize(value, sKey, sTab.."\t");
else
local sValue = (type(value) == "string") and string.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)
local handle = io.open(file,"w+")
handle:write(Serialize(table, tablename))
handle:flush()
handle:close()
end
function LoadFromFile(file)
local handle = io.open(file,"r")
if (handle ~= nil) then
loadstring(handle:read("*all"))
handle:flush()
handle:close()
end
end
Best regards, nErBoS
Along the same lines, this just is just a bit of IO to store some text in a log file. :)
filename = "../logs/nuked.log";
function StoreFlooder(curUser, sFloodUser)
data = sFloodUser.." was flooded by "..curUser.. " on "..os.date("%d").."."..os.date("%m").."."..os.date("%y").." at "..os.date("%H")..":"..os.date("%M")..":"..os.date("%S").."\r\n";
local file = io.open( filename, "a" );
file:write( data );
file:close();
return 1;
end
This expects 2 usernames as input, but is easy to change to suit your needs. No prizes for guessing which script i put this in, but i won't post the rest of the script, because the origial author (Phatty) removed the LUA 4 version cause of abuse. I agree with and respect his decision. :)
*** Edited to remove smilies from code.
Hi,
Made a little update in the code above.
Best regards, nErBoS
this is a bit more open way, so you can use it for several log files.
function StoreLog(file, msg)
local fFile = io.open( file, "a" );
fFile:write( msg);
fFile:close();
return 1;
end
StoreLog("../logs/nuked.log", sFloodUser.." was flooded by "..curUser.. " on "..os.date("%d").."."..os.date("%m").."."..os.date("%y").." at "..os.date("%H")..":"..os.date("%M")..":"..os.date("%S").."\r\n")
plop
Thanks Plop. :)
That looks a lot neater as well as being more versitile. :)
Good job nErBoS and plop! :D
As i haven't looked into lua5 too much, i appreciate this howto very much. But one question: in lua5 you first create the whole string to write, then, and only then you write it, don't you?
Hi,
If i have understood your question well.... no you can write whatever you want using the write(string) until you close the file, you can call the times you want the write(string), it will start write in the point that it last wrote, like in LUA 4.
Best regards, nErBoS
here's another one for Lua 5 ..
for loading just use dofile("file.lua")
--================================================================---
--==-- Save file/table Functions ================================---
--================================================================---
Save_Serialize = function(tTable, sTableName, hFile, sTab)
sTab = sTab or "";
hFile:write(sTab..sTableName.." = {\n" );
for key, value in tTable do
local sKey = (type(key) == "string") and string.format("[%q]",key) or string.format("[%d]",key);
if(type(value) == "table") then
Save_Serialize(value, sKey, hFile, sTab.."\t");
else
local sValue = (type(value) == "string") and string.format("%q",value) or tostring(value);
hFile:write( sTab.."\t"..sKey.." = "..sValue);
end
hFile:write( ",\n");
end
hFile:write( sTab.."}");
end
Save_File = function(file,table , tablename )
local hFile = io.open (file , "w")
Save_Serialize(table, tablename, hFile);
hFile:close()
end
Typhoon?
i have a question it possible to make a table in table
example :
tabl1 have
usr.nick
usr.sher
usr.connect
and usr.connect to be a table whit evry time hu usr connect to the hub
QuoteOriginally posted by dragos_sto
i have a question it possible to make a table in table
example :
tabl1 have
usr.nick
usr.sher
usr.connect
and usr.connect to be a table whit evry time hu usr connect to the hub
nested tables are possible and can go as deep as you have memory.
plop
m8 10x fot answer , but how to save to file and read from file ?
in this way i need help
QuoteOriginally posted by dragos_sto
m8 10x fot answer , but how to save to file and read from file ?
in this way i need help
Way depends on how you wanna use, will it be a table like
table={"a","b","c"}
(a. k. a. array)
or like
table={["a"]=1,["b"]=2}
?
Anyway, example for both:
function ReadFile(file)
local table={} -- create table
local f=io.open(file,"r") -- open the file for reading
if f then -- if file exists
for line in f:lines() do -- parse lines in file one by one
table.tinsert(line) -- insert every line
-- OR:
--table[line]=1 --useful if you wanna dfo hashing later
end
end
end
function ReadFile(file) -- another way, for file like: a|b (separator can be custom of course)
local table={} -- create table
local f=io.open(file,"r") -- open the file for reading
if f then -- if file exists
for line in f:lines() do -- parse lines in file one by one
local _,_,k,v=string.find(line,"(.+)|(.+)")
table[k]=v
end
end
end
function SaveFile(table,file)
local f=io.open(file,"w+") -- open the file for writing
if f then -- this is always true imho
for key,value in table do -- ok, now parse the table contents one by one
f:write(key) -- and write them to file
--OR
-- f:write(key.."|"..value) -- it outputs: a|b (see above)
end
end
end
Sorry, edited under linux with kedit, no debugging or highlighting was possible, but hope this will help. :)
-- // edit
No syntax errors in my post, found SciTE (http://www.scintilla.org/SciTE.html). :))
bastya_elvtars 10x for answer , but pls be more specific
to my problem pls
how to read and write table whit other table inside them .
my problem it i dont achieved to read or to write the
second table inside of the big table
i look in other script , but i dont find any sollutions to my problem
Quotetable1 have
usr.nick
usr.sher
usr.connect
and usr.connect to be a table2 whit each time when usr connect to the hub
user={
nick="nick",
share=11654310242B
connect=1
}
-- however there is still user.bConnected... :)
Not a so hard-to-save string. :P
i whant to make something like this
user={
nick="nick",
share=11654310242B,
connect={ [1]="os.date"%h:%m"",
[2]="os.date"%h:%m"",
......................................................................
[n]="os.date"%h:%m"",
},
}
QuoteOriginally posted by dragos_sto
i whant to make something like this
user={
nick="nick",
share=11654310242B,
connect={ [1]="os.date"%h:%m"",
[2]="os.date"%h:%m"",
......................................................................
[n]="os.date"%h:%m"",
},
}
Eh, eh. When I release LawMaker, we can go back to this, now I have no time.
ok 10x i will wait a sollution to my problem
check my freshstuff or texter bot's (latest versions).
think you'll find some nice hints/info there.
plop