Thread Tools Display Modes
10-17-14, 10:50 PM   #1
Blooblahguy
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: Oct 2009
Posts: 18
Issues with table indices

As the title suggest, having issues with some tables/indices that i'm using in an addon i'm working on and I'm absolutely stumped.

I'll try my best to explain it before linking my code. But basically I have a table format of table_name[itemID][raidIndex] = stored_frame. Looping through the table using table.foreach returns this structure, however accessing the table using the above string returns a nil index error. Here's my code to hopefully provide some clarity.

I set my table here for each entry
Code:
table.insert(entry_frames, itemID, {[raidIndex] = entryframe})
Now later I'm trying to fetch that entryframe on it and run code on it.

Code:
table.foreach(entry_frames, function(a, b)
	print("a: "..a) -- returns the itemID
	-- b returns the table below
	table.foreach(b, function(raidIndex, bb)
		print("aa: "..aa) - -returns the raid index
		--print("bb: "..bb) returns nil
	end)
end)

print(entry_frames[itemID][raidIndex]) -- throws fatal index ? error
Now what I want to do it both remove this frame/indices from the table and then run a function on the frame to return it to my frame cache

Code:
table.remove(entry_frames, itemID[raidIndex])
destroy_frame(_G["vote_entry:"..itemID..":"..raidIndex])
Of course these both fail as is. Any insights?
  Reply With Quote
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

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » Issues with table indices

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