Load and Save Tables
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

Load and Save Tables

Started by nErBoS, 27 February, 2005, 03:07:41

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

nErBoS

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
--## nErBoS Spot ##--

Pothead

#1
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.

nErBoS

Hi,

Made a little update in the code above.

Best regards, nErBoS
--## nErBoS Spot ##--

plop

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
http://www.plop.nl lua scripts/howto\'s.
http://www.thegoldenangel.net
http://www.vikingshub.com
http://www.lua.org

>>----> he who fights hatred with hatred, drives the spreading of hatred <----<<

Pothead

Thanks Plop. :)
That looks a lot neater as well as being more versitile. :)

Dag

Good job nErBoS and plop!  :D

bastya_elvtars

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?
Everything could have been anything else and it would have just as much meaning.

nErBoS

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
--## nErBoS Spot ##--

Typhoon

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?



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

plop

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
http://www.plop.nl lua scripts/howto\'s.
http://www.thegoldenangel.net
http://www.vikingshub.com
http://www.lua.org

>>----> he who fights hatred with hatred, drives the spreading of hatred <----<<

dragos_sto

#11
m8 10x fot answer , but how to save to file and read from file ?
in this way i need help

bastya_elvtars

#12
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. :))
Everything could have been anything else and it would have just as much meaning.

dragos_sto

#13
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

bastya_elvtars

user={
	nick="nick",
	share=11654310242B
	connect=1
	}

-- however there is still user.bConnected... :)

Not a so hard-to-save string. :P
Everything could have been anything else and it would have just as much meaning.

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"",
                      },
}

bastya_elvtars

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.
Everything could have been anything else and it would have just as much meaning.

dragos_sto

ok 10x i will wait  a sollution to my problem

plop

check my freshstuff or texter bot's (latest versions).
think you'll find some nice hints/info there.

plop
http://www.plop.nl lua scripts/howto\'s.
http://www.thegoldenangel.net
http://www.vikingshub.com
http://www.lua.org

>>----> he who fights hatred with hatred, drives the spreading of hatred <----<<

SMF spam blocked by CleanTalk