PtokaX forum

Archive => Archived 5.0 boards => Request for scripts => Topic started by: DorianG on 19 May, 2005, 19:14:02

Title: An Other Serialize Function
Post by: DorianG on 19 May, 2005, 19:14:02
I wanna save this array in a file, but with the normal function of Serialize it's impossible.. Someone can help me with a right funtion..

This is an axample of array that i wanna save and load:

Array = {
["Set"] = {["Set1"] = 1, ["Set2"] = 1, ["Set3"] = 1, ["Set4"] = 1, ["Set5"] = 0, ["Set6"] = 0},
}
Title:
Post by: Dessamator on 19 May, 2005, 19:53:24
impossible?, hmm i doubt that, i used a standard serialize and it works::

Array = {
["Set"] = {["Set1"] = 1, ["Set2"] = 1, ["Set3"] = 1, ["Set4"] = 1, ["Set5"] = 0, ["Set6"] = 0},
}
fArray = "fArray.log"


function Main()
SaveToFile(fArray , Array  , "Array")
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 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
                dofile(file)
handle:flush()
handle:close()
        end

end
Title:
Post by: plop on 20 May, 2005, 18:05:18
QuoteOriginally posted by DorianG
I wanna save this array in a file, but with the normal function of Serialize it's impossible.. Someone can help me with a right funtion..

This is an axample of array that i wanna save and load:

Array = {
["Set"] = {["Set1"] = 1, ["Set2"] = 1, ["Set3"] = 1, ["Set4"] = 1, ["Set5"] = 0, ["Set6"] = 0},
}

thats an table.
try the save functions from my latest texter or freshstuff script's.

plop