WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   The order of saved variables (https://www.wowinterface.com/forums/showthread.php?t=52212)

Yukyuk 04-19-15 09:36 AM

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.     },

Rilgamon 04-19-15 09:43 AM

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

Phanx 04-20-15 05:07 AM

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.

Resike 04-20-15 07:37 AM

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

Yukyuk 04-20-15 11:13 AM

Thanks for the help.
Based on the information you people have given I will continue :)


All times are GMT -6. The time now is 04:04 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI