Author Topic: Write to table in file... bcdc++  (Read 3034 times)

0 Members and 1 Guest are viewing this topic.

Offline BottledHate

  • Triple Ace
  • **
  • Posts: 244
  • Karma: +2/-0
Write to table in file... bcdc++
« 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.
Homepage: www.bottledhate.webhop.org

Compiling  Lua scripts is LAME!!!!!

PtokaX forum

Write to table in file... bcdc++
« on: 11 July, 2004, 03:39:59 »

Offline Corayzon

  • Lord
  • ***
  • Posts: 464
  • Karma: +1/-0
(No subject)
« Reply #1 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.

Code: [Select]
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:-
Code: [Select]
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

Offline BottledHate

  • Triple Ace
  • **
  • Posts: 244
  • Karma: +2/-0
(No subject)
« Reply #2 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++
« Last Edit: 11 July, 2004, 06:26:38 by BottledHate »
Homepage: www.bottledhate.webhop.org

Compiling  Lua scripts is LAME!!!!!

Offline Corayzon

  • Lord
  • ***
  • Posts: 464
  • Karma: +1/-0
(No subject)
« Reply #3 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
« Last Edit: 11 July, 2004, 06:32:36 by Corayzon »

Offline BottledHate

  • Triple Ace
  • **
  • Posts: 244
  • Karma: +2/-0
(No subject)
« Reply #4 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......
« Last Edit: 11 July, 2004, 07:04:29 by BottledHate »
Homepage: www.bottledhate.webhop.org

Compiling  Lua scripts is LAME!!!!!

Offline NotRabidWombat

  • Lord
  • ***
  • Posts: 384
  • Karma: +2/-0
(No subject)
« Reply #5 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
« Last Edit: 11 July, 2004, 07:39:45 by NotRabidWombat »


I like childish behavior. Maybe this post will be deleted next.

Offline BottledHate

  • Triple Ace
  • **
  • Posts: 244
  • Karma: +2/-0
(No subject)
« Reply #6 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.
Homepage: www.bottledhate.webhop.org

Compiling  Lua scripts is LAME!!!!!

Offline NotRabidWombat

  • Lord
  • ***
  • Posts: 384
  • Karma: +2/-0
(No subject)
« Reply #7 on: 11 July, 2004, 08:09:50 »
You're welcome.

-NotRabidWombat


I like childish behavior. Maybe this post will be deleted next.

Offline BottledHate

  • Triple Ace
  • **
  • Posts: 244
  • Karma: +2/-0
(No subject)
« Reply #8 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

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
Homepage: www.bottledhate.webhop.org

Compiling  Lua scripts is LAME!!!!!

PtokaX forum

(No subject)
« Reply #8 on: 03 September, 2004, 06:48:05 »