Thread Tools Display Modes
12-11-16, 10:24 AM   #1
Lolzen
An Aku'mai Servant
 
Lolzen's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2008
Posts: 36
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?
  Reply With Quote
12-11-16, 11:44 AM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
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)
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 12-12-16 at 12:53 AM.
  Reply With Quote
12-11-16, 11:50 AM   #3
Tosaido
A Fallenroot Satyr
 
Tosaido's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2015
Posts: 23
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

Last edited by Tosaido : 12-11-16 at 11:56 AM.
  Reply With Quote
12-11-16, 11:56 AM   #4
Lolzen
An Aku'mai Servant
 
Lolzen's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2008
Posts: 36
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.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Copy table contents to another table with exustung content

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