WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Copy table contents to another table with exustung content (https://www.wowinterface.com/forums/showthread.php?t=54915)

Lolzen 12-11-16 10:24 AM

Copy table contents to another table with exustung content
 
So I've had trouble finding a solution to this on my own.
The idea is to be able to support "plugins" to my addon.

Disregard the unfinished state, as it is primary a learning project for myself.
As seen i have my table named Stylemeter.datamodules here.
Now i want to be able to write let's say a Damage Taken module which may look like something like this:
Code:

dt = {
        ["Damage Taken"] = {
                ["activated"] = true,
                        ["strings"] = {
                        ["SWING_DAMAGE"] = {
                                "arg12", --damagetaken swing
                        },
                        ["RANGE_DAMAGE"] = {
                                "arg15", --damagetaken range
                        },
                        ["SPELL_DAMAGE"] = {
                                "arg15", --damagetaken spell
                        },
                        ["ENVIRONMENTAL_DAMAGE"] = {
                                "arg13", --damagetaken environmental
                        },
                },
        },
}

how can i copy the contents of dt into StyleMeter.datamodules?
I've come across deepcopy functions in my research, but all the do seems to make a direct copy of the table, i want to actually insert these keys and values to an existing one.

Or is my approach here completely in the wrong direction?

Fizzlemizz 12-11-16 11:44 AM

A generic recursive table copy function:
Code:

function Copy_Table(src, dest)
        for index, value in pairs(src) do
                if type(value) == "table" then
                        dest[index] = {}
                        Copy_Table(value, dest[index])
                else
                        dest[index] = value
                end
        end
end

Your destination can be a sub-table in another table.

Code:

ns.datamodules.DamageTaken = {}
Copy_Table(dt, ns.datamodules.DamageTaken)


Tosaido 12-11-16 11:50 AM

Lua Code:
  1. --[[
  2.  
  3. If you would like to slip the whole table called ["Damage_Taken"](remove the space) into Stylemeter.datamodules then you can just use
  4.  
  5. --]]
  6.  
  7. Stylemeter.datamodules.Damage_Taken = dt.Damage_Taken
  8.  
  9. --[[
  10.  
  11. or if the name of the table is dynamic/unknown then try this
  12.  
  13. --]]
  14.  
  15. for TableName, TableValue in pairs(dt) do
  16.     Stylemeter.datamodules[TableName] = TableValue
  17. end
  18.  
  19. --[[
  20.  
  21. or if you want to only copy the values within the table ["strings"] then try
  22.  
  23. NOTE: This is provided that a table with these names already exists
  24.  
  25. --]]
  26.  
  27. for StringName, StringValue in pairs(dt.Damage_Taken.strings) do
  28.     Stylemeter.datamodules.Damage_Taken.strings[StringName] = StringValue
  29. end
  30.  
  31. --[[
  32.  
  33. or if you want to only copy the values within the table ["strings"] and the name is dynamic/unknown then try
  34.  
  35. NOTE: This is provided that a table with these names already exists
  36.  
  37. --]]
  38.  
  39. for TableName, TableValue in pairs(dt) do
  40.  
  41.     for StringName, StringValue in pairs(TableValue.strings) do
  42.         Stylemeter.datamodules[TableName].strings[StringName] = StringValue
  43.     end
  44. end

Lolzen 12-11-16 11:56 AM

Thank you very much, everyone!
Seems so simple, yet i've spent hours on figuring that out without success.

Defenitely some elegant ways to accomplish this, that's neat.


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

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