Thread Tools Display Modes
01-02-17, 03:47 PM   #1
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
copy SV table with additions

It occurs to me that users of SmartRes2 have added their own phrases to the table SmartRes2.db.profile.randChatTbl, and with my rewrite, I am moving that table into a module called Chat.

I found a copy table function that will work well enough, assuming that Chat.db.profile.randChatTbl was empty, but it is not empty -- it has its own default values, 25 of which duplicate the original SR2 built-in defaults.

I could copy it straight up with Chat.db.profile.randChatTbl starting as an empty table, but that won't work if the user resets the profile, as there won't be default values to restore. Chat.blah needs default values.

Therein lies the trouble. Not only do I NOT want to create duplicate entries, I also do not care if the user deleted some or all of SR2's defaults (there is an option to do so). I am only interested in copying the values that are new or different. So now I am confused, mainly because the examples linked are not fully documented on what and how they are doing things, and most of the variables are not very descriptive.

tl;dr: copy only user-defined phrases from SmartRes2.db.profile.randChatTbl to Chat.db.profile.randChatTbl and then wipe SmartRes2.db.profile.randChatTbl because it will become defunct.

How do I go about doing this?

Last edited by myrroddin : 01-02-17 at 03:50 PM. Reason: why I can't start empty.
  Reply With Quote
01-02-17, 04:05 PM   #2
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
Wow, rereading that post, and I don't think it made sense, so an example. Keep in mind that I would have absolutely no idea what phrases the user added.
Lua Code:
  1. -- exists in both SR2.db and Chat.db, therefore ignore this phrase during copy
  2. "%p is bringing %t back to life!"
  3.  
  4. -- user-defined, so copy this to Chat.db
  5. "By the Light, I am resurrecting %t"
  6.  
  7. -- in SmartRes2.db, but user removed, but that doesn't matter because it exists as a default phrase in Chat.db anyway
  8. "Once again, %p pulls %t and their bacon out of the fire."
  Reply With Quote
01-02-17, 04:31 PM   #3
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
would this work?
Lua Code:
  1. local function CopyAndDeleteTable(source)
  2.     if #source = 0 then return end
  3.     local dest = Chat.db.profile.randChatTbl
  4.     for sk, sv in pairs(source) do
  5.         for ck cv in pairs(dest) do
  6.             if not string.match(dest, sv) then
  7.                 table.insert(dest, sv)
  8.             end
  9.         end
  10.     end
  11.     wipe(source)
  12. end
  13.  
  14. function Chat:OnInitialize()
  15.     CopyAndDeleteTable(SmartRes2.db.profile.randChatTbl)
  16. end
  Reply With Quote
01-02-17, 05:02 PM   #4
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
This might work better, because I think it is just a simple table, and not key, value. Maybe?
Lua Code:
  1. local function CopyAndDeleteTable(source)
  2.     local dest = Chat.db.profile.randChatTbl
  3.     for i = 1, #source do
  4.         for c = 1, #dest do
  5.             if not string.match(dest[c], i) then
  6.                 table.insert(dest, i)
  7.             end
  8.         end
  9.     end
  10.     wipe(source)
  11. end
  Reply With Quote
01-02-17, 09:54 PM   #5
Gethe
RealUI Developer
 
Gethe's Avatar
Premium Member
Featured
Join Date: Sep 2008
Posts: 942
As they are, both would likely not work as you expect. In your first version, 'dest' is a table and would thus raise in an error. It should probably be 'cv'. In the second version, 'i' is a number which is probably not your intended comparison. It should probably be 'source[i]'.

Regarding the use of pairs vs #table. It really depends on whether or not the table order matters. There may be some slight performance benefit from the second method, but it probably won't be noticeable at this scale. For convenience, there is also ipairs that works similarly to pairs, but respects table index.

Putting those aside, the function would still not work as you describe because as written it would add the string from 'source' multiple times for each mismatch. This would result in a lot of duplicate entries. The way to do it is to traverse the entire destination table before adding the new string. This assures that 1) If the string already exists, but is at the end of the table, it isn't added again prior to that check, and 2) It is only added once.

Lua Code:
  1. local function CopyAndDeleteTable(source)
  2.     local dest = Chat.db.profile.randChatTbl
  3.     for si, sv in ipairs(source) do  -- Using ipairs here mostly as an example of usage.
  4.         local hasMatch
  5.         for ci, cv in ipairs(dest) do
  6.             if string.match(cv, sv) then
  7.                 hasMatch = true
  8.                 break -- We found a dupe, no need to go farther
  9.             end
  10.         end
  11.         if not hasMatch then
  12.             table.insert(dest, sv)
  13.         end
  14.     end
  15.     wipe(source)
  16. end

Finally, are you planning to do anything else with the old table? If not, using wipe() will only remove keys and indexes on the table, the table itself will still exist. To delete it, you would have to reassign the table as nil. In this case you would have to move the delete part out of CopyAndDeleteTable because assigning nil to 'source' would only delete that reference, not the actual table.


All together:
Lua Code:
  1. local function CopyTable(source)
  2.     local dest = Chat.db.profile.randChatTbl
  3.     for si, sv in ipairs(source) do  -- Using ipairs here mostly as an example of usage.
  4.         local hasMatch
  5.         for ci, cv in ipairs(dest) do
  6.             if string.match(cv, sv) then
  7.                 hasMatch = true
  8.                 break -- We found a dupe, no need to go farther
  9.             end
  10.         end
  11.         if not hasMatch then
  12.             table.insert(dest, sv)
  13.         end
  14.     end
  15. end
  16.  
  17. function Chat:OnInitialize()
  18.     CopyTable(SmartRes2.db.profile.randChatTbl)
  19.     SmartRes2.db.profile.randChatTbl = nil  -- delete the now unused table
  20. end

Hope this helps!
__________________
Knowledge = Power; Be OP

  Reply With Quote
01-02-17, 10:21 PM   #6
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
It helps immensely, as you can see from my prior attempts! Thank you very much.

1) no, I was not planning on using the old table, and do want it removed entirely from the DB, so your last bit should work.
2) maybe just because I'm tired, but the break's position isn't going to prevent the parser from continuing to find non-matches to copy, right?
  Reply With Quote
01-03-17, 07:06 PM   #7
Gethe
RealUI Developer
 
Gethe's Avatar
Premium Member
Featured
Join Date: Sep 2008
Posts: 942
Originally Posted by myrroddin View Post
2) maybe just because I'm tired, but the break's position isn't going to prevent the parser from continuing to find non-matches to copy, right?
No, break will only exit the innermost loop.
__________________
Knowledge = Power; Be OP

  Reply With Quote
01-03-17, 08:09 PM   #8
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
return would exit out completely.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
01-03-17, 10:38 PM   #9
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
Once again, thank you! I have to stop looking at code, and/or writing code when I'm tired

Hopefully the users, when it all gets released, appreciate these little things. I certainly didn't have to try to salvage their old settings.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » copy SV table with additions


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