Nested tables and serialize

Posted by Shaun Biggs on Tue 05 Jun 2007 06:29 AM — 5 posts, 19,898 views.

USA #0
I'm poking around with trying to save some data between sessions for a plugin of mine, and I ran into a wall trying to deal with nested tables. Here's an example of some of the test data I was using:

/temp = {"qwerty","asdfgh","zxvcbn"}
/print( serialize.save("temp", temp))
temp = {}
  temp[1] = "qwerty"
  temp[2] = "asfdgh"
  temp[3] = "zxvcbn" table: 0116D540

/test = { {1,2,3},{4,5,6},{7,8,9} }
/print( serialize.save("test", test))
Run-time error
World: Aardwolf - Balaam
Immediate execution
C:\Program Files\MUSHclient\lua\serialize.lua:116: Invalid name 'test[1]' for table
stack traceback:
        [C]: in function 'assert'
        C:\Program Files\MUSHclient\lua\serialize.lua:116: in function 'save_item'
        C:\Program Files\MUSHclient\lua\serialize.lua:169: in function 'save_item'
        C:\Program Files\MUSHclient\lua\serialize.lua:61: in function 'save'
        [string "Command line"]:1: in main chunk

The same error is thrown each time I try to use a nested table, no matter what values I try. I know I'm missing something blatantly obvious here, but I can't figure out what that would be.
USA #1
If memory serves this is a small problem in Nick's serialization code. Search for the error message and you'll find it. Then make sure it looks like this:

      assert (string.find (name, "^[_%a][_%a%d%.%[%]\"\"]*$") 
              and not lua_reserved_words [name], 
              "Invalid name '" .. name .. "' for table")



I get:


[david@thebalrog:~/development/lib-lua/lua5.1/lua]$ lua
Lua 5.1.1 Copyright (C) 1994-2006 Lua.org, PUC-Rio
> require 'serialize'
> test = { {1,2,3},{4,5,6},{7,8,9} }
> print(serialize.save("test", test))
test = {}
test[1] = {}
test[1][1] = 1
test[1][2] = 2
test[1][3] = 3
test[2] = {}
test[2][1] = 4
test[2][2] = 5
test[2][3] = 6
test[3] = {}
test[3][1] = 7
test[3][2] = 8
test[3][3] = 9 table: 0x64c430
>



This might be worth fixing in the stock serialize.lua distribution, actually.
USA #2
Thank you very much. Serialize works beautifully now. Unfortunately, I can't quite release my plugins with this unless it's fixed in the standard distribution as well. I suppose I could put in an error catcher and have instructions on how to fix it.
USA #3
You could also try changing the global serialize table's function to your own, but that's kind of hackish. I think it would be easiest if the standard file got fixed. Or you could distribute a fixed version and ask people to use that one instead until the main one gets fixed.
Australia Forum Administrator #4
I have updated the serialize.lua function as described by David - for version 4.08 onwards.

For the meantime, it is something you could simply incorporate in your plugin, rather than relying on the separate serialize.lua file.