SaveTable woes
 

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

SaveTable woes

Started by bastya_elvtars, 25 February, 2007, 16:58:28

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

bastya_elvtars

Hey peeps, I have this code and it saves an array in a weird format:
Code: lua
return {{[1] = {2},},
{[1] = [[music]],[2] = [[bastya@home]],[3] = [[02/24/2007]],[4] = [[something]],},
}


Can somebody please explain why it writes {[1] = {2},}, out? It breaks the entire thing.
Everything could have been anything else and it would have just as much meaning.

bastya_elvtars

#1
There is not even such stuff in my array. The array consists of tabloes as values:
Code: lua
AllStuff=
{
  {[1] = [[music]],[2] = [[bastya@home]],[3] = [[02/24/2007]],[4] = [[something]],}
  {[1] = [[music]],[2] = [[bastya@home]],[3] = [[02/24/2007]],[4] = [[something2]],}
}

OK, found the problem, tables saved by this method should be loaded like this: mytbl=table.load("file") - I have just called table.load("file"), completely ignored the return thing. That table inside is used internally by the routine.

BTW does anyone know who wrote this? I think chill did, but unsure...
Everything could have been anything else and it would have just as much meaning.

plop

is it freshstuff 3?
then it could be me, older versions were made by chill.

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

bastya_elvtars

No, this code is from the lua-users wiki. There is no information on the author.
Everything could have been anything else and it would have just as much meaning.

CHILLCODE?

#4
this code is meant to save also tables where you have an index as a table e.g.

t = { "test" }
t2 = {} t2[t] = { "test" }

and it encloses the strings, so you don't have to worry about what you save and speacial chars as \r or \n are saved properly, these make problems if you only save it with  sting.format( "%q", string ).

So in your case you only need a simple serialize function, but if you one day have cyclic references in the table or tables as index, you will not be able to use any normal serialize script anymore. So this script basically saves anything that can be saved. As said I had some help from the pickle.lua script that you will also find on the wiki, but that one has a bug since it shows you a quite different table after loading again, I tried this code with lots of tables and never could find any differences between the saved and the loaded one, and thats what you want. But this script will need more time in your case to save and load, since you could also use a simple serialize.

Ahm and yup I wrote it with help from the pickle.lua script as said :)

bastya_elvtars

Quote from: CHILLCODE? on 26 February, 2007, 12:01:38
But this script will need more time in your case to save and load, since you could also use a simple serialize.

Yes, I need it for exact data persistence (especially multiline).

Quote from: CHILLCODE? on 26 February, 2007, 12:01:38
Ahm and yup I wrote it with help from the pickle.lua script as said :)
OK, then pickle is free to use, how about this, may I use it? :-)
Everything could have been anything else and it would have just as much meaning.

CHILLCODE?

sure its free, else it wouldn't have no use :), I think anything posted on the lua wiki page is for free usage.

bastya_elvtars

Quote from: CHILLCODE? on 26 February, 2007, 14:48:16
sure its free, else it wouldn't have no use :), I think anything posted on the lua wiki page is for free usage.

Thanks! :)
Found a bug: it goes mad when I try to save strings starting or ending with square brackets. I have circuvented it, but will look into a real solution.
Everything could have been anything else and it would have just as much meaning.

CHILLCODE?

hey can you give me the entrie that fucks up or the table even

I tried the script with this code and I thaught it was okey, also shows some breaktes

Code: lua
function Main()

	print( "Serialise Test" )

	t = {}

	t.a = 1
	t.b = 2
	t.c = {}
	-- self reference
	t.c.a = t
	--should change t.a to 4
	t.c.a.a = 4

	t.func = function() end

	-- table as index
	t.f = {  [{ a = 5, b = 7, }] = "[[ hellooooo ]] ]]", [{ 1,2,3, m = 5, 5,6,7 }] = { a = 1 }, }

	t.i = { [{ [{ a = 10 }] = 6, }] = 4 }

	t.enc = "[[ [=[ hello world"

	t.x = "[ [ \"hello \r\n world \'] ]]"

	-- references
	t.c.d = t.c.a

	print( "Before Save\n\n" )
	for i,v in pairs( t) do
		print( i, v )
	end
	for i,v in pairs( t.f) do
		if type( i ) == "table" then
			print( table.maxn( i ) )
			for i2,v2 in pairs( i) do
				print( "\t", i2, v2 )
			end
		else
			print( i, v )
		end
	end
	
	table.save( t, "t.txt" )

	t = table.load( "t.txt" )

	print("\n\nAfter Save\n\n" )

	for i,v in pairs( t) do
		print( i, v )
	end
	for i,v in pairs( t.f) do
		if type( i ) == "table" then
			print( table.maxn( i ) )
			for i2,v2 in pairs( i) do
				print( "\t", i2, v2 )
			end
		else
			print( i, v )
		end
	end

	table.save( t, "t2.txt" )
	io.read()
end
Main()


so if you could just tell me what the item looks like that messes up I'll try and fix it

bastya_elvtars

Code: lua
{[1] = [[music]],[2] = [[(TGA-OP)Darkstar?]],[3] = [[11/07/2004]],[4] = [[andrea bocelli - andrea [2004]]],},
Everything could have been anything else and it would have just as much meaning.

CHILLCODE?

#10
hey how could I miss that, ah well thx a lot, i fixed it hopefully now :)

Code: lua
--[[
	TABLE.SAVE 0.4

	table.save( table, file ) 0.4
		Save Table to File
		Functions, Userdata and indices of these will not be saved
		Strings will be converted to enclosed long brakets, makes it easier to save regardingly special chars,
		now hopefull for sure thx to 'bastya_elvtars'
		References are preserved

	table.load( file )
		returns a previously saved table

]]--

function table.save( t, sfile )
	local tables = {}
	table.insert( tables, t )
	local lookup = { [t] = 1 }
	local file = io.open( sfile, "w" )
	file:write( "return {" )
	for i,v in ipairs( tables ) do
		table.pickle( v, file, tables, lookup )
	end
	file:write( "}" )
	file:close()
end

function table.pickle( t, file, tables, lookup )
	file:write( "{" )
	for i,v in pairs( t) do
		-- escape functions
		if type( v ) ~= "function" and type( v ) ~= "userdata" then
			-- handle index
			if type( i ) == "table" then
				if not lookup[i] then
					table.insert( tables, i )
					lookup[i] = table.maxn( tables )
				end
				file:write( "[{"..lookup[i].."}] = " )
			else
				local index = ( type( i ) == "string" and "[ "..string.enclose( i, 50 ).." ]" ) or string.format( "[%d]", i )
				file:write( index.." = " )
			end
			-- handle value
			if type( v ) == "table" then
				if not lookup[v] then
					table.insert( tables, v )
					lookup[v] = table.maxn( tables )
				end
				file:write( "{"..lookup[v].."}," )
			else
				local value =  ( type( v ) == "string" and string.enclose( v, 50 ) ) or tostring( v )
				file:write( value.."," )
			end
		end
	end
	file:write( "},\n" )		
end

-- enclose string by long brakets ( string, maxlevel )
function string.enclose( s, maxlevel )
	s = "["..s.."]"
	local level = 0
	while 1 do
		if maxlevel and level == maxlevel then
			error( "error: maxlevel too low, "..maxlevel )
		-- 
		elseif string.find( s, "%["..string.rep( "=", level ).."%[" ) or string.find( s, "]"..string.rep( "=", level ).."]" ) then
			level = level + 1
		else
			return "["..string.rep( "=", level )..s..string.rep( "=", level ).."]"
		end
	end
end

function table.load( sfile )
	local tables = dofile( sfile )
	if tables then
		local tcopy = {}
		table.unpickle( tables[1], tables, tcopy )
		return tcopy
	end
end

function table.unpickle( t, tables, tcopy, pickled )
	pickled = pickled or {}
	pickled[t] = tcopy
	for i,v in pairs( t ) do
		local i2 = i
		if type( i ) == "table" then
			local pointer = tables[ i[1] ]
			if pickled[pointer] then
				i2 = pickled[pointer]
			else
				i2 = {}
				table.unpickle( pointer, tables, i2, pickled )
			end
		end
		local v2 = v
		if type( v ) == "table" then
			local pointer = tables[ v[1] ]
			if pickled[pointer] then
				v2 = pickled[pointer]
			else
				v2 = {}
				table.unpickle( pointer, tables, v2, pickled )
			end
		end
		tcopy[i2] = v2
	end
end

bastya_elvtars

It works fine. Thank you.
Everything could have been anything else and it would have just as much meaning.

SMF spam blocked by CleanTalk