View Single Post
10-17-14, 11:06 PM   #2
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
table.foreach was deprecated years ago; if you're looping over a purely indexed table (all keys are sequential integers starting from 1) you should do this:

Code:
for i = 1, #myTable do
     local v = myTable[i]
     -- do stuff here
end
If you're looping over a hash table, or a table with numeric keys that aren't sequential (ie. there are holes in the sequence) or don't start from 1, use this:

Code:
for k, v in pairs(myTable) do
    -- do stuff here
end
You can use pairs on an indexed table too, but it's much slower than the #-based method I posted, so you shouldn't use it.

As for your actual problem, you're doing this:

Code:
table.insert(entry_frames, itemID, {[raidIndex] = entryframe})
... which will make your table look like this:

Code:
entry_frames = {
     [6948] = { 12 = <frame ref> },
     [90067] = { 14 = <frame ref> },
}
That's not an indexed table. The keys are integers, but they're not sequential, and they don't start from 1, so calling them indices is incorrect.

Code:
table.remove(entry_frames, itemID[raidIndex])
Do you have a table called "itemID" in which the keys are raid indices and the values are item IDs? If not, this line will not do what you think it does. You shouldn't really be using table.remove on a non-indiced table, even if it will work on numeric keys; just do:

Code:
entry_frames[itemID][raidIndex] = nil
... which refers to "itemID" as a variable as I think you're intending to do. Assuming that's your intention, your existing code should have been giving you an error message about attempting to index a non-table value. If you don't have an error display, get Bugger. It will make your addon-writing life so much easier when you can see exactly what's wrong and where.

Code:
destroy_frame(_G["vote_entry:"..itemID..":"..raidIndex])
Assuming you're actually giving your frames global names that look like "vote_entry:6948:12" this will work, but that's really horrific. I'd suggst not giving your frames any global names, and doing this instead:

Code:
-- use the frame reference from the table first:
destroy_frame(entry_frames[itemID][raidIndex])
-- THEN remove the frame reference from the table:
entry_frames[itemID][raidIndex] = nil
__________________
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