PtokaX forum

Development Section => HOW-TO's => Topic started by: BottledHate on 11 July, 2004, 03:39:59

Title: Write to table in file... bcdc++
Post by: BottledHate on 11 July, 2004, 03:39:59
i'm havning trouble writting to a table in a file... it's just a simple counter really..  when somene says something i want it to count the number of times it is said and save it to a file each time it's said, so the number is saved over time.  there may be 5 or 6 different things i'm counting.. hence the need for a table.


thanks for the help, as a newb to lua i need it!  i'm sure this is easy for most.
Title:
Post by: Corayzon on 11 July, 2004, 05:42:28
hey BottledHate,

You can use these functions to simply and quickly save and load tables from your harddisk.

function saveTable(tTable, sTableName, sFile)
assert(type(tTable) == "table", "saveTable(tTable, sTableName, sFile) - tTable must be a table!")
assert(type(sTableName) == "string", "saveTable(tTable, sTableName, sFile) - sTableName must be a string!")
assert(type(sFile) == "string", "saveTable(tTable, sTableName, sFile) - sFile must be a string!")
local hFile = openfile(sFile, "w")
writeTable(tTable, sTableName, hFile)
closefile(hFile)
hFile = nil
end

function writeTable(tTable, sTableName, hFile, sTab)
assert(type(tTable) == "table", "tTable must be a table!")
assert(type(sTableName) == "string", "sTableName must be a string!")

sTab = sTab or ""
write(hFile, sTab .. sTableName .. " = \r\n" .. sTab .. "{")
local iStart = 0

for key, value in tTable do

if iStart == 1 then
write(hFile, ",\r\n")
else
write(hFile, "\r\n")
iStart = 1
end

local sKey = (type(key) == "string") and format("[%q]",key) or format("[%d]",key)

if(type(value) == "table") then
writeTable(value, sKey, hFile, sTab .. "\t")
else
local sValue = (type(value) == "string") and format("%q",value) or tostring(value)
write(hFile, sTab .. "\t" .. sKey .. " = " .. sValue)
end
end
write(hFile, "\r\n" .. sTab .. "}")
end

function loadTable(sFile)
assert(dofile(sFile), "Error readfing table: " .. sFile)
end

example:-
tTable = {[1] = "Matt", [2] = "Jess", ["Jess"] = "loser", ["matt"] = "uncool"}
saveTable(tTable, "tTable", "testfile.txt")
tTable = nil
loadTable("testfile.txt")
SendToAll("tTable = " .. type(tTable))

hope it helps

noza
Title:
Post by: BottledHate on 11 July, 2004, 06:21:55
thanks for the quick reply.. i'll try that out now...


-----is that for bcdc or ptokax?

bcdc doesn't like the openfile and closefile commands.. nil value.. :/

need to do this in bcdc++
Title:
Post by: Corayzon on 11 July, 2004, 06:31:44
hey BottledHate,

i didnt realise u wanted it for bcdc. This should work on it cause its just lua, but maybe bc is lua5. which will ofcourse not work with lua4.

Ill get onto the developers about it and see what the problem is your talking bout.

neways...give it a tri and let me know the outcome

noza
Title:
Post by: BottledHate on 11 July, 2004, 06:45:41
yeah.. no go on that scripit..

attempt to call a global 'openfile' (a nil value)

edit: it runs fine in titmouse.. just not bcdc.

thanks for your help... i'll drop by your hub and see if i can catch you there... are you ever in snoozes private hub? i see your link is run through his dns......
Title:
Post by: NotRabidWombat on 11 July, 2004, 07:37:41
The latest version of BCDC++ comes with a serialization function in: libsimplepickle.lua

I presume this comes from the more extensive version which can be found here: http://www.dekorte.com/Software/Lua/LuaPickle/Download/Pickle/Pickle.lua

How do you get pickle from serialize anyway?

-NotRabidWombat
Title:
Post by: BottledHate on 11 July, 2004, 07:57:08
thanks!!! i never even looked in the libsimplepickle.lua... it is loaded with the formatting.lua.

to answer my own question..... to save a table to disk with bcdc++... (currently running .403)  with the libsimplepickle.lua loaded:


tTable = {[1] = "Matt", [2] = "Jess", ["Jesssssssssssssssssssssssssssssss"] = "loser", ["matt"] = "uncool"}

pickle.store ( "testing123.txt", tTable )






yeah.. simple i know.
Title:
Post by: NotRabidWombat on 11 July, 2004, 08:09:50
You're welcome.

-NotRabidWombat
Title:
Post by: BottledHate on 03 September, 2004, 06:48:05
lol.. 3 short months ago i didn't know shit when i
started this thread.  and here is where i am now with
reading/writting files.... THREAD (http://board.univ-angers.fr/thread.php?threadid=2654&boardid=4&sid=cc91def25c73d235d9c716d7dd42aee6&page=1)

i've learned so much from the great help of the people
 here.  in case i havn't thanked everyone before..
thank you for your time, knowledge, and patience. :)

-BH