Thread Tools Display Modes
08-08-09, 09:42 PM   #1
Morant
A Deviate Faerie Dragon
 
Morant's Avatar
Join Date: Feb 2009
Posts: 18
Question Updating Table Values (I think that is what I am asking)

I have a table for defaults. A copy of which is saved in the WTF folder and loaded as MainOptions.

Code:
local MainDefaults {
    ["MainOnOff"] = 1,
    ["OptionWhatever"] = 1,
...

    ["Version"] = "0.21",
}
I have implemented a statement that checks:

Code:
if (type(MainOptions["Version"]) == "string") or (MainOptions["Version"] < MainDefaults["Version"]) then
UpdateVersion()
end
The first few versions of this I have released have the version set to 1.0b - not a number so I want to account for those by checking to the type"string". The reason the new value is a decimal is to signify beta in a number (below 1) without using string types.

Now for the trouble:

I have tried in vain but can not figure out why..

Code:
function VersionUpdate()	
	if not MainOptions then
		MainOptions = MainDefaults
	end
	local MainOptionsTemp = MainDefaults
	for k,v in pairs (MainOptions) do
		if (MainOptions[k] ~= MainOptionsTemp[k]) then
			MainOptionsTemp[k] = MainOptions[k]
		end
	end
MainOptions = MainOptionsTemp	
MainOptions["Version"] = MainDefaults["Version"] 
MainOptionsTemp = nil
end
Will not update the "Version" value correctly. Everything else is fine I think, not tested fully yet though. Can't get past this. I think I am trying to update the value of "Version" in the table:MainOptions incorrectly and I can not locate any reading material that helps.

I found tinsert() but that doesn't seem to be right.

Comments? Assistance?


/cheers
__________________
...because I can... THAT'S why...
  Reply With Quote
08-09-09, 12:15 PM   #2
Akryn
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 479
Two things immediately jump out:

1. "pairs (MainOptions)" shouldn't have a space -- I'm actually not sure if that matters in Lua, I've never tried it.
2. Table variables are pointers. If you do this:
Code:
local a = {1,2,3}
local b = a
b[2] = "elephant"
print(a[2])
you will get "elephant" -- I think that's your main problem. You might also be calling your update function before the variables load?
  Reply With Quote
08-09-09, 12:41 PM   #3
Foxlit
A Warpwood Thunder Caller
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 91
Code:
local MainOptionsTemp = MainDefaults -- < This is a reference, not a copy
for k,v in pairs (MainOptions) do
	if (MainOptions[k] ~= MainOptionsTemp[k]) then
		MainOptionsTemp[k] = MainOptions[k] -- < This runs for k="Version", setting MainDefaults.Version = MainOptions.Version
	end
end
-- ... 
MainOptions["Version"] = MainDefaults["Version"] -- < Has no effect, as MainDefaults.Version == MainOptions.Version already.
__________________
... and you do get used to it, after a while.
  Reply With Quote
08-10-09, 02:06 PM   #4
Morant
A Deviate Faerie Dragon
 
Morant's Avatar
Join Date: Feb 2009
Posts: 18
Thank you both. I have thought on this and here is what I come up with... Please correct me if I am out there somewhere...

you will get "elephant" -- I think that's your main problem.
Let me understand

Code:
local MainOptionsTemp = MainDefaults -- < This is a reference, not a copy
better.

I reason then if you want a copy of the data contained in [k] then you would have to:
for k,v ...

local TempVariable = MainOptions[K]

... update current defaults and then check back to the TempVariable for what the previous setting was and if it is not the same as the defaults now loaded, update it..

MainOptions[k] = TempVariable

Yes? Even if not exactly right for this particular code block, my logic of it is my concern.

NOTE: Something discovered is that some of my variable are able to be set to = 0. In the SavedVariables .lua this equates to no entry for that particular tables [k]... thus when comparing the user settings to defaults the ones set to zero are not processed as they are "nil". Have to rethink this with that in mind. But the above really helps in understanding what I am asking it to do.
__________________
...because I can... THAT'S why...
  Reply With Quote
08-10-09, 04:55 PM   #5
Akryn
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 479
You don't have to write the function yourself any more, fortunately:

b = CopyTable(a)
  Reply With Quote
11-01-10, 06:09 AM   #6
Crowfeather
A Fallenroot Satyr
 
Crowfeather's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2008
Posts: 28
Found this via search as I had a very similar Problem. CopyTable solved that problem but unfortunatly it also caused memory consumption of my addon to spike. It looks like instead of overwriting an existing table (if it exist) it creates a new one everytime it is used. So if I did something like self.button1.resources = CopyTable(self.resources) where resources is a rather large object table once every half second or so it allocated new memory for the table everytime it's called. I guess it'd be garbage collected eventually but still it is kind of wasteful if my 130 KB AddOn uses a several megabytes of memory after a few minutes.

I resorted to copy the table by hand as Morant suggested in his last post and it works but it's kind of annoying and if there're values in the target table that are nil in the source table I have to delete them explicitly. I'm propably going to code my own version of CopyTable that does all this but before I do wanted to ask if I missed something important or if someone else has already done that work.

Last edited by Crowfeather : 11-01-10 at 06:11 AM.
  Reply With Quote
11-01-10, 03:10 PM   #7
Xubera
A Cobalt Mageweaver
 
Xubera's Avatar
AddOn Author - Click to view addons
Join Date: May 2009
Posts: 207
well nevermind this post is over a year old but

Code:
local table1 = {
    settings = false,
    option1 = true,
    option2 = 0.5,
    option3 = "LEFT",
    option4 = nil
}
local table2 = {}
--to copy the table, we use pairs
for k,v in pairs(table1) do
    table2[k] = v
end
this would make table 2 have every key and value as table1 except for option4, because not even table1 has option4 (because its nil).

if you wanted to do something like to check if a user has saved variables in the beginning you would do

Code:
local defaultOptions = {
    option1 = false
    option2 = true
    option3 = 0.5
    option4 = "LEFT"
}

for k,v in pairs(defaultOptions) do
    if myAddonDB[k] ~= nil then
       myAddonDB[k] = v
    end
end
the reason it is myAddonDB[k] ~= nil instead of not myAddonDB[k] is because both false and nil would return true for not myAddonDB[k] therefore overriding any option set to false. with nil, it will set any option that doesnt exist. Using this method, anytime you set your settings to the false position, you need to save it as false, and not nil for this reason
__________________
Chat Consolidate is the solution to any out of control trade chat. Ignore lines, throttle chat, consolidate posts!Follow the link to find out how!

▲ ▲ WoWInterface wont let me triforce >.>
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Updating Table Values (I think that is what I am asking)


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