Jump to content




textutils.unserialize() rearranging items in table

lua api

4 replies to this topic

#1 JackMacWindows

  • Members
  • 20 posts

Posted 24 July 2016 - 03: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

#2 Luca_S

  • Members
  • 387 posts
  • LocationGermany

Posted 24 July 2016 - 08:36 PM

The problem is probably not the serialization I just checked it, everything works fine, what do you use to display the texts?

#3 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,452 posts

Posted 24 July 2016 - 09: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.

#4 JackMacWindows

  • Members
  • 20 posts

Posted 26 July 2016 - 03:19 AM

View PostLyqyd, on 24 July 2016 - 09:09 PM, said:

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
I print with:
for l,x in pairs(menulist) do
    write("   " .. l)
  end
where menulist is a table with the names.

#5 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,452 posts

Posted 26 July 2016 - 03: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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users