This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
JackMacWindows's profile picture

textutils.unserialize() rearranging items in table

Started by JackMacWindows, 24 July 2016 - 02:00 PM
JackMacWindows #1
Posted 24 July 2016 - 04:00 PM
Hello,
I am working on an OS for ComputerCraft. In it, I have a serialized table in a file that is read by the program and the keys are displayed in a row. I want the keys to be displayed in a specific order, but they are always re-arranged into an order I do not want.
In the file, the keys are in the order {"File", "Edit", "View", "Go", "Window"}.
In the row, the text is in the order {"View", "Edit", "Window", "File", "Go"}.
I cannot change the order in the program because the program has to be portable for other files like this one. Is this a bug? Is there anyway I can fix this problem? I am running CraftOS 1.7.
Thanks, Jack
Luca_S #2
Posted 24 July 2016 - 09:36 PM
The problem is probably not the serialization I just checked it, everything works fine, what do you use to display the texts?
Lyqyd #3
Posted 24 July 2016 - 10:09 PM
Let's see your actual table, please. If you're using textual keys, there is no "order" of elements inherent in the table.
JackMacWindows #4
Posted 26 July 2016 - 04:19 AM
Let's see your actual table, please. If you're using textual keys, there is no "order" of elements inherent in the table.
Here is the whole table:
Spoiler
{
  File = {
    ["New Folder"] = "-newfol",
    Print = "-print",
    ["New Window"] = "-newwin",
  },
  Edit = {
    Cut = "_cut",
    Paste = "_paste",
    Undo = "-undo",
    Copy = "_copy",
  },
  View = {
    Icons = "-icons",
    List = "-list",
  },
  Go = {
    Forward = "-forward",
    ["To Folder..."] = "-goto",
    Back = "-back",
  },
  Window = {
    Hide = "_hide",
    Close = "_close",
  },
}
I print with:

for l,x in pairs(menulist) do
    write("   " .. l)
  end
where menulist is a table with the names.
Lyqyd #5
Posted 26 July 2016 - 04:27 AM
Your table is using strings ("View", "Window", etc.) as keys, so there is no inherent order to the table. If you want the table to be in a specific order, you might have to structure your entries a little differently:


{
  {
	name = "File",
	contents = {
	  ["New Folder"] = "-newfol",
	  Print = "-print",
	  ["New Window"] = "-newwin",
	}
  },
  {
	name = "Edit",
	contents = {
	  Cut = "_cut",
	  Paste = "_paste",
	  Undo = "-undo",
	  Copy = "_copy",
	}
  },
}

Note that the inner tables (which contain the "name" and "contents" keys) will be implicitly assigned numeric keys when they are declared this way.