Thread Tools Display Modes
04-19-15, 09:36 AM   #1
Yukyuk
A Chromatic Dragonspawn
 
Yukyuk's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2015
Posts: 179
The order of saved variables

Working on my first addon so am quite new a this.
I am using AceBd to set up my database and that seems to work ok.
But my problem is the order of the of fields in my tables as you can see in the example.

Anybody know how I can solve this?


Lua Code:
  1. ["char"] = {
  2.         ["Yukyuk - Moonglade"] = {
  3.             ["data_char_start_historia"] = 1429372016,
  4.             ["data_char_entering"] = 9,
  5.             ["data_char_leaving"] = 9,
  6.         },
  7.         ["Valadrasil - Moonglade"] = {
  8.             ["data_char_leaving"] = 1,
  9.             ["data_char_entering"] = 1,
  10.             ["data_char_start_historia"] = 1429453141,
  11.         },
  12.     },
  Reply With Quote
04-19-15, 09:43 AM   #2
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
Lua-tables dont know an order and they dont keep one. If you want to make sure to have an order you'd have to sort them the time you access them or you'd use numbered indexes.
For the later you have ipairs to iterate over
http://www.lua.org/pil/7.3.html
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
04-20-15, 05:07 AM   #3
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Based on what you posted, I assume you will be accessing values like this:

MyAddon.db.char.data_char_entering

...in which case there is no need for any order. As Rilgamon said, the Lua programming language has no concept of an order for the key/value pairs in a table. The order in which they are written out in the saved variables file is essentially random, and has no effect on anything.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
04-20-15, 07:37 AM   #4
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
If you want to read them sorted your could use spairs:

Lua Code:
  1. local function spairs(t, order)
  2.     local keys = { }
  3.     for k in pairs(t) do
  4.         keys[#keys + 1] = k
  5.     end
  6.     if order then
  7.         table.sort(keys, function(a, b)
  8.             return order(t, a, b)
  9.         end)
  10.     else
  11.         table.sort(keys)
  12.     end
  13.     local i = 0
  14.     return function()
  15.         i = i + 1
  16.         if keys[i] then
  17.             return keys[i], t[keys[i]]
  18.         end
  19.     end
  20. end
  Reply With Quote
04-20-15, 11:13 AM   #5
Yukyuk
A Chromatic Dragonspawn
 
Yukyuk's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2015
Posts: 179
Thanks for the help.
Based on the information you people have given I will continue
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » The order of saved variables

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off