Thread Tools Display Modes
01-23-14, 08:04 PM   #1
Aanson
A Flamescale Wyrmkin
Join Date: Aug 2009
Posts: 124
Originally Posted by Seerah View Post
Ah, yes. And this. I honestly didn't scroll to see how many events you had in your table.
See my other post in this thread.
__________________
__________________
  Reply With Quote
01-24-14, 04:14 AM   #2
Rainrider
A Firelord
AddOn Author - Click to view addons
Join Date: Nov 2008
Posts: 454
In case it's still unclear:
Code:
for i = 1, select("#", unpack(self.Events)) do
will result in what you seem to have expected from select. Don't do it that way though
  Reply With Quote
01-24-14, 07:14 AM   #3
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 793
I'm sorry but this makes me cringe.

After I saw 'for i = 1, select("#", unpack(self.Events)) do' I had to write back!

Why not use the standard way of writing a table loop?

Code:
local events = {"A", "B", "C"}
for i = 1, #events do
  self:RegisterEvent(events[i])
end
The select function is great when fetching a specific argument returned from a function that returns a list of arguments, and using "#" returns how many arguments were returned.

Code:
function Events()
  return "A", "B", "C"
end

for k, v in ipairs({Events()}) do
  self:RegisterEvent(v)
end
Also this one works, not ideal but works. You encapsulate the returned arguments in a table and it will return the same table as in the first example. You then iterate it directly using ipairs.

In any case I find this to be a much better approach than unpacking the table to then use select "#" on it to iterate just over the index.
__________________
Profile: Curse | Wowhead
  Reply With Quote
01-24-14, 01:16 PM   #4
Rainrider
A Firelord
AddOn Author - Click to view addons
Join Date: Nov 2008
Posts: 454
It seems 3 lines are too much to read, Vlad. What the TE seemed to expect select to return is actually the result of the code snippet I posted. Knowing how unpack works, helps understanding how select works. That was the point of my previous point. Maybe I failed at it, maybe not. I also stated (s)he should not use it like that. So please read before you criticize.
  Reply With Quote
01-24-14, 05:55 PM   #5
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 793
To be honest I couldn't quite tell the purpose at first, so I hastily posted what my first gut feeling told me was the right response - sorry if I made you feel bad in any way!
__________________
Profile: Curse | Wowhead
  Reply With Quote
01-24-14, 06:11 PM   #6
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Rainrider View Post
It seems 3 lines are too much to read, Vlad. What the TE seemed to expect select to return is actually the result of the code snippet I posted. Knowing how unpack works, helps understanding how select works. That was the point of my previous point. Maybe I failed at it, maybe not. I also stated (s)he should not use it like that. So please read before you criticize.
The problem with posting "bad" examples and saying "don't use this" is that -- as you yourself are complaining -- people don't actually read the "don't use this" part, and end up using your cringe-worthy anti-example as real code in their real addon. I make a habit of never posting functional code that should not actually be used, and I strongly recommend you (and everyone else) adopt this habit, too. If you want to show how something works, make up an example that wouldn't make you want to gouge your eyes out if you saw it in a real addon.
__________________
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
01-25-14, 01:44 PM   #7
Rainrider
A Firelord
AddOn Author - Click to view addons
Join Date: Nov 2008
Posts: 454
If a person comes to a medium for written communication to ask questions, I assume they do read. If they don't, it's all their fault.
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Getting the number of indexes in an array.


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