Write to table in file... bcdc++
 

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

Write to table in file... bcdc++

Started by BottledHate, 11 July, 2004, 03:39:59

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

BottledHate

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!!!!!

Corayzon

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

BottledHate

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

Compiling  Lua scripts is LAME!!!!!

Corayzon

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

BottledHate

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

Compiling  Lua scripts is LAME!!!!!

NotRabidWombat

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


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

BottledHate

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!!!!!

NotRabidWombat

You're welcome.

-NotRabidWombat


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

BottledHate

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!!!!!

SMF spam blocked by CleanTalk