Thread Tools Display Modes
08-29-13, 08:35 AM   #1
suicidalkatt
A Rage Talon Dragon Guard
 
suicidalkatt's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 331
Getting and Setting a tabled value

Basically I have a table of strings that are the same as the index to a saved variables table.

I'm not sure how to go about writing the proper function to set and get the values in of the saved variable table using the table of strings.

Lua Code:
  1. local example = {
  2.     profile = {
  3.         helper = {
  4.             enable = true,
  5.         }
  6.     }
  7. }
  8.  
  9. local index = {
  10.     "profile",
  11.     "helper",
  12.     "enable"
  13. }
  14.  
  15. local function GetValue(t) -- This Seems to work
  16.     if type(t) == "table" then
  17.         local DB
  18.         for k, v in next, t do
  19.             if not DB then
  20.                 DB = example[t[k]]
  21.             else
  22.                 DB = DB[t[k]]
  23.             end
  24.         end
  25.         return DB
  26.     else
  27.         return example[t]
  28.     end
  29. end
  30.  
  31. local function SetValue(t, val)
  32.      -- Do things here???
  33. end
  Reply With Quote
08-29-13, 09:08 AM   #2
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Phanx has some awesome functions for this.

Check this thread:

http://www.wowinterface.com/forums/s...ad.php?t=44038

However i might missunderstood what you really want to do.

Last edited by Resike : 08-29-13 at 09:12 AM.
  Reply With Quote
08-29-13, 09:19 AM   #3
suicidalkatt
A Rage Talon Dragon Guard
 
suicidalkatt's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 331
Originally Posted by Resike View Post
Phanx has some awesome functions for this.

Check this thread:

http://www.wowinterface.com/forums/s...ad.php?t=44038

However i might missunderstood what you really want to do.
That's just a standard copyTable function that preserves the initial 'default' table.

My 'GetValue' and 'SetValue' would only affect a single value within the main table with all the various values.

You'd get to the single value within the main table with a small index table in the example.

My only issue is setting that value with an index table without having to do something less 'clean' in my eyes like this:

Lua Code:
  1. local function SetValue(t,val)
  2.     if type(t) == "table" then
  3.         local a,b,c = t[1],t[2],t[3]
  4.         if c then
  5.             example[a][b][c] = val
  6.         elseif b then
  7.             example[a][b] = val
  8.         elseif a then
  9.             example[a] = val
  10.         end
  11.     else
  12.         example[t] = val
  13.     end
  14. end
  Reply With Quote
08-29-13, 09:35 AM   #4
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
What do you think about something like that:

Lua Code:
  1. master = {
  2.     key = {
  3.         key2 = "Hi!"
  4.     }
  5. }
  6. child = { }
  7. setmetatable(child, {__index = master})
  8.  
  9. child.key.key2 = "NOHIFORU"
  10.  
  11. print(child.key.key2) -- "NOHIFORU!"
  Reply With Quote
08-29-13, 10:10 AM   #5
suicidalkatt
A Rage Talon Dragon Guard
 
suicidalkatt's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 331
I'm fairly sure using a metatable would be the best, but I've not worked much with it to really get the just of how to put it into code.
  Reply With Quote
08-29-13, 10:48 AM   #6
Vrul
A Scalebane Royal Guard
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 404
Here a simple version of what I think you want.
Code:
local function GetValue(table, keys)
    local value = table
    for index = 1, #keys do
        value = value[keys[index]]
    end
    return value
end

local function SetValue(table, keys, value)
    for index = 1, #keys - 1 do
        table = table[keys[index]]
    end
    table[keys[#keys]] = value
end
  Reply With Quote
08-29-13, 11:25 AM   #7
suicidalkatt
A Rage Talon Dragon Guard
 
suicidalkatt's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 331
Originally Posted by Vrul View Post
Here a simple version of what I think you want.
Code:
local function GetValue(table, keys)
    local value = table
    for index = 1, #keys do
        value = value[keys[index]]
    end
    return value
end

local function SetValue(table, keys, value)
    for index = 1, #keys - 1 do
        table = table[keys[index]]
    end
    table[keys[#keys]] = value
end
Interesting approach, I'll have to alter it a bit to be able to pass the DB into the functions.
  Reply With Quote
08-29-13, 05:45 PM   #8
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Why do you need to pass the db into the get/set functions? It's not like the path to the db (for AceAddon/AceDB) changes during gameplay; it's always located at MyAddon.db.profile, so just use that in your functions.

Also, the functions Vrul posted will only work if your options table structure matches your db table structure exactly.

Finally, AceConfig doesn't limit you to one setter function per options table. Rather than trying to shoehorn every possible scenario into one function, you are probably better off writing more specific functions tailored for each option group, or even each individual option.
__________________
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
08-30-13, 08:02 AM   #9
suicidalkatt
A Rage Talon Dragon Guard
 
suicidalkatt's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 331
Originally Posted by Phanx View Post
Why do you need to pass the db into the get/set functions? It's not like the path to the db (for AceAddon/AceDB) changes during gameplay; it's always located at MyAddon.db.profile, so just use that in your functions.

Also, the functions Vrul posted will only work if your options table structure matches your db table structure exactly.

Finally, AceConfig doesn't limit you to one setter function per options table. Rather than trying to shoehorn every possible scenario into one function, you are probably better off writing more specific functions tailored for each option group, or even each individual option.
I'm aware, I didn't end up trying to pass it into the function. Just declared locally instead. I do have other functions depending on what they're going to be intended for (color values for example).

I've been completely restructuring a lot of code so simplification feels amazing.

After a bit of researching metatabling would not have worked as cleanly.
  Reply With Quote
08-30-13, 03:14 PM   #10
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Using a single mega-function that handles every possible case is generally not a "simplification" over using multiple individual functions that each handle a few specific cases. If you don't look at your code for 6 months, are you going to remember what all the loops and other convoluted code in your mega-function even do? If not, you probably shouldn't write it that way in the first place.
__________________
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
09-01-13, 03:36 AM   #11
suicidalkatt
A Rage Talon Dragon Guard
 
suicidalkatt's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 331
Originally Posted by Phanx View Post
Using a single mega-function that handles every possible case is generally not a "simplification" over using multiple individual functions that each handle a few specific cases. If you don't look at your code for 6 months, are you going to remember what all the loops and other convoluted code in your mega-function even do? If not, you probably shouldn't write it that way in the first place.
I've since dropped down from 9 or 10 functions down to 5, it consolidated quite a few.

Most of those 'few functions' you spoke about are directly in the options table and are not a locally declared.

I feel that the change to this function was the right choice.
  Reply With Quote
09-01-13, 10:14 PM   #12
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Well, as long as you're aware that what you're doing is purely stylistic, and offers no real functional benefit, I guess you can feel however you want about it.
__________________
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

WoWInterface » Developer Discussions » Lua/XML Help » Getting and Setting a tabled value


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