Author Topic: SaveTable woes  (Read 4134 times)

0 Members and 1 Guest are viewing this topic.

Offline bastya_elvtars

  • Forum God
  • ****
  • Posts: 3 745
  • Karma: +173/-7
  • The rock n' roll doctor
    • The FreshStuff3 Site
SaveTable woes
« on: 25 February, 2007, 16:58:28 »
Hey peeps, I have this code and it saves an array in a weird format:
Code: Lua
  1. return {{[1] = {2},},
  2. {[1] = [[music]],[2] = [[bastya@home]],[3] = [[02/24/2007]],[4] = [[something]],},
  3. }

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.

PtokaX forum

SaveTable woes
« on: 25 February, 2007, 16:58:28 »

Offline bastya_elvtars

  • Forum God
  • ****
  • Posts: 3 745
  • Karma: +173/-7
  • The rock n' roll doctor
    • The FreshStuff3 Site
Re: SaveTable woes
« Reply #1 on: 25 February, 2007, 17:42:31 »
There is not even such stuff in my array. The array consists of tabloes as values:
Code: Lua
  1. AllStuff=
  2. {
  3.   {[1] = [[music]],[2] = [[bastya@home]],[3] = [[02/24/2007]],[4] = [[something]],}
  4.   {[1] = [[music]],[2] = [[bastya@home]],[3] = [[02/24/2007]],[4] = [[something2]],}
  5. }

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...
« Last Edit: 25 February, 2007, 17:49:59 by bastya_elvtars »
Everything could have been anything else and it would have just as much meaning.

Offline plop

  • Forum God
  • ****
  • Posts: 2 464
  • Karma: +37/-0
Re: SaveTable woes
« Reply #2 on: 25 February, 2007, 20:05:42 »
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 <----<<

Offline bastya_elvtars

  • Forum God
  • ****
  • Posts: 3 745
  • Karma: +173/-7
  • The rock n' roll doctor
    • The FreshStuff3 Site
Re: SaveTable woes
« Reply #3 on: 25 February, 2007, 20:41:20 »
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.

Offline CHILLCODE?

  • Scripter
  • Member
  • ******
  • Posts: 31
  • Karma: +21/-0
Re: SaveTable woes
« Reply #4 on: 26 February, 2007, 12:01:38 »
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 :)
« Last Edit: 26 February, 2007, 12:05:37 by CHILLCODE? »

Offline bastya_elvtars

  • Forum God
  • ****
  • Posts: 3 745
  • Karma: +173/-7
  • The rock n' roll doctor
    • The FreshStuff3 Site
Re: SaveTable woes
« Reply #5 on: 26 February, 2007, 13:55:22 »
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).

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.

Offline CHILLCODE?

  • Scripter
  • Member
  • ******
  • Posts: 31
  • Karma: +21/-0
Re: SaveTable woes
« Reply #6 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.

Offline bastya_elvtars

  • Forum God
  • ****
  • Posts: 3 745
  • Karma: +173/-7
  • The rock n' roll doctor
    • The FreshStuff3 Site
Re: SaveTable woes
« Reply #7 on: 07 March, 2007, 00:23:55 »
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.

Offline CHILLCODE?

  • Scripter
  • Member
  • ******
  • Posts: 31
  • Karma: +21/-0
Re: SaveTable woes
« Reply #8 on: 07 March, 2007, 14:42:51 »
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
  1. function Main()
  2.  
  3.         print( "Serialise Test" )
  4.  
  5.         t = {}
  6.  
  7.         t.a = 1
  8.         t.b = 2
  9.         t.c = {}
  10.         -- self reference
  11.         t.c.a = t
  12.         --should change t.a to 4
  13.         t.c.a.a = 4
  14.  
  15.         t.func = function() end
  16.  
  17.         -- table as index
  18.         t.f = {  [{ a = 5, b = 7, }] = "[[ hellooooo ]] ]]", [{ 1,2,3, m = 5, 5,6,7 }] = { a = 1 }, }
  19.  
  20.         t.i = { [{ [{ a = 10 }] = 6, }] = 4 }
  21.  
  22.         t.enc = "[[ [=[ hello world"
  23.  
  24.         t.x = "[ [ \"hello \r\n world \'] ]]"
  25.  
  26.         -- references
  27.         t.c.d = t.c.a
  28.  
  29.         print( "Before Save\n\n" )
  30.         for i,v in pairs( t) do
  31.                 print( i, v )
  32.         end
  33.         for i,v in pairs( t.f) do
  34.                 if type( i ) == "table" then
  35.                         print( table.maxn( i ) )
  36.                         for i2,v2 in pairs( i) do
  37.                                 print( "\t", i2, v2 )
  38.                         end
  39.                 else
  40.                         print( i, v )
  41.                 end
  42.         end
  43.        
  44.         table.save( t, "t.txt" )
  45.  
  46.         t = table.load( "t.txt" )
  47.  
  48.         print("\n\nAfter Save\n\n" )
  49.  
  50.         for i,v in pairs( t) do
  51.                 print( i, v )
  52.         end
  53.         for i,v in pairs( t.f) do
  54.                 if type( i ) == "table" then
  55.                         print( table.maxn( i ) )
  56.                         for i2,v2 in pairs( i) do
  57.                                 print( "\t", i2, v2 )
  58.                         end
  59.                 else
  60.                         print( i, v )
  61.                 end
  62.         end
  63.  
  64.         table.save( t, "t2.txt" )
  65.         io.read()
  66. end
  67. Main()
  68.  

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

Offline bastya_elvtars

  • Forum God
  • ****
  • Posts: 3 745
  • Karma: +173/-7
  • The rock n' roll doctor
    • The FreshStuff3 Site
Re: SaveTable woes
« Reply #9 on: 07 March, 2007, 14:57:45 »
Code: Lua
  1. {[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.

Offline CHILLCODE?

  • Scripter
  • Member
  • ******
  • Posts: 31
  • Karma: +21/-0
Re: SaveTable woes
« Reply #10 on: 09 March, 2007, 13:59:42 »
hey how could I miss that, ah well thx a lot, i fixed it hopefully now :)

Code: Lua
  1. --[[
  2.         TABLE.SAVE 0.4
  3.  
  4.         table.save( table, file ) 0.4
  5.                 Save Table to File
  6.                 Functions, Userdata and indices of these will not be saved
  7.                 Strings will be converted to enclosed long brakets, makes it easier to save regardingly special chars,
  8.                 now hopefull for sure thx to 'bastya_elvtars'
  9.                 References are preserved
  10.  
  11.         table.load( file )
  12.                 returns a previously saved table
  13.  
  14. ]]--
  15.  
  16. function table.save( t, sfile )
  17.         local tables = {}
  18.         table.insert( tables, t )
  19.         local lookup = { [t] = 1 }
  20.         local file = io.open( sfile, "w" )
  21.         file:write( "return {" )
  22.         for i,v in ipairs( tables ) do
  23.                 table.pickle( v, file, tables, lookup )
  24.         end
  25.         file:write( "}" )
  26.         file:close()
  27. end
  28.  
  29. function table.pickle( t, file, tables, lookup )
  30.         file:write( "{" )
  31.         for i,v in pairs( t) do
  32.                 -- escape functions
  33.                 if type( v ) ~= "function" and type( v ) ~= "userdata" then
  34.                         -- handle index
  35.                         if type( i ) == "table" then
  36.                                 if not lookup[i] then
  37.                                         table.insert( tables, i )
  38.                                         lookup[i] = table.maxn( tables )
  39.                                 end
  40.                                 file:write( "[{"..lookup[i].."}] = " )
  41.                         else
  42.                                 local index = ( type( i ) == "string" and "[ "..string.enclose( i, 50 ).." ]" ) or string.format( "[%d]", i )
  43.                                 file:write( index.." = " )
  44.                         end
  45.                         -- handle value
  46.                         if type( v ) == "table" then
  47.                                 if not lookup[v] then
  48.                                         table.insert( tables, v )
  49.                                         lookup[v] = table.maxn( tables )
  50.                                 end
  51.                                 file:write( "{"..lookup[v].."}," )
  52.                         else
  53.                                 local value =  ( type( v ) == "string" and string.enclose( v, 50 ) ) or tostring( v )
  54.                                 file:write( value.."," )
  55.                         end
  56.                 end
  57.         end
  58.         file:write( "},\n" )           
  59. end
  60.  
  61. -- enclose string by long brakets ( string, maxlevel )
  62. function string.enclose( s, maxlevel )
  63.         s = "["..s.."]"
  64.         local level = 0
  65.         while 1 do
  66.                 if maxlevel and level == maxlevel then
  67.                         error( "error: maxlevel too low, "..maxlevel )
  68.                 --
  69.                 elseif string.find( s, "%["..string.rep( "=", level ).."%[" ) or string.find( s, "]"..string.rep( "=", level ).."]" ) then
  70.                         level = level + 1
  71.                 else
  72.                         return "["..string.rep( "=", level )..s..string.rep( "=", level ).."]"
  73.                 end
  74.         end
  75. end
  76.  
  77. function table.load( sfile )
  78.         local tables = dofile( sfile )
  79.         if tables then
  80.                 local tcopy = {}
  81.                 table.unpickle( tables[1], tables, tcopy )
  82.                 return tcopy
  83.         end
  84. end
  85.  
  86. function table.unpickle( t, tables, tcopy, pickled )
  87.         pickled = pickled or {}
  88.         pickled[t] = tcopy
  89.         for i,v in pairs( t ) do
  90.                 local i2 = i
  91.                 if type( i ) == "table" then
  92.                         local pointer = tables[ i[1] ]
  93.                         if pickled[pointer] then
  94.                                 i2 = pickled[pointer]
  95.                         else
  96.                                 i2 = {}
  97.                                 table.unpickle( pointer, tables, i2, pickled )
  98.                         end
  99.                 end
  100.                 local v2 = v
  101.                 if type( v ) == "table" then
  102.                         local pointer = tables[ v[1] ]
  103.                         if pickled[pointer] then
  104.                                 v2 = pickled[pointer]
  105.                         else
  106.                                 v2 = {}
  107.                                 table.unpickle( pointer, tables, v2, pickled )
  108.                         end
  109.                 end
  110.                 tcopy[i2] = v2
  111.         end
  112. end
  113.  
« Last Edit: 09 March, 2007, 15:09:32 by CHILLCODE? »

Offline bastya_elvtars

  • Forum God
  • ****
  • Posts: 3 745
  • Karma: +173/-7
  • The rock n' roll doctor
    • The FreshStuff3 Site
Re: SaveTable woes
« Reply #11 on: 09 March, 2007, 15:57:25 »
It works fine. Thank you.
Everything could have been anything else and it would have just as much meaning.

PtokaX forum

Re: SaveTable woes
« Reply #11 on: 09 March, 2007, 15:57:25 »