Thread Tools Display Modes
01-20-12, 07:09 AM   #1
Lur
A Defias Bandit
AddOn Author - Click to view addons
Join Date: Jan 2012
Posts: 3
How to obtain vale count from table?

Hello everyone.
I got a small problem writing a little addon.

There is GetGossipAvailableQuests() frunction

Let's imagine we are talking with a NPC with 3 available quests. So if I want to iterate all returned quests, I write something like:
Code:
local q = {GetGossipAvailableQuests()} 
for i=1, #q, 5 do 
   print(q[i])
end
The question is:
1. why #q returns correct count while documentation says that next after first nil value index are returned even if there are another non-nil values
2. why select("#", q) doesn't work correct then?

Thanks a lot!
  Reply With Quote
01-20-12, 07:56 AM   #2
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
Edit: Forget my answer
__________________
The cataclysm broke the world ... and the pandas could not fix it!

Last edited by Rilgamon : 01-20-12 at 07:59 AM.
  Reply With Quote
01-20-12, 08:42 AM   #3
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
1: Any nil values are ignored in table construction. If, after construction, you explicitly set a value to nil, the rule you spoke of would take effect.

2: "If index is a number, returns all arguments after argument number index. Otherwise, index must be the string "#", and select returns the total number of extra arguments it received." If you are assigning the return value of select() to a single variable, it stands to reason that the rest of the return values would be discarded.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
01-20-12, 10:16 AM   #4
Lur
A Defias Bandit
AddOn Author - Click to view addons
Join Date: Jan 2012
Posts: 3
Originally Posted by Torhal View Post
1: Any nil values are ignored in table construction. If, after construction, you explicitly set a value to nil, the rule you spoke of would take effect.
Don't you mind to provide a link? I can't found anything like that in 5.1 reference ...


2: "If index is a number, returns all arguments after argument number index. Otherwise, index must be the string "#", and select returns the total number of extra arguments it received." If you are assigning the return value of select() to a single variable, it stands to reason that the rest of the return values would be discarded.
So, select("#" doesn't return the pair count in the table? It's just returns something like
Code:
local l = {[1]=1,[2]=2,[3]=3}
select("#", l)  -- = 1
select("#", a,b,c)  -- = 3
  Reply With Quote
01-20-12, 07:19 PM   #5
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Lur View Post
So, select("#" doesn't return the pair count in the table? It's just returns something like
Code:
local l = {[1]=1,[2]=2,[3]=3}
select("#", l)  -- = 1
select("#", a,b,c)  -- = 3
Yes. Also:
Code:
local t = { [1] = "first", [2] = "second", [3] = "third" }
#t --> 3

t["foo"] = "bar"
#t --> 3 -- non-indexed table entries don't count

t[2] = nil
#t --> 1 -- because you created a gap
  Reply With Quote
01-22-12, 01:48 AM   #6
Lur
A Defias Bandit
AddOn Author - Click to view addons
Join Date: Jan 2012
Posts: 3
Originally Posted by Phanx View Post
Yes. Also:
Thanks a lot. But why

Code:
local t1 =  {1,2, nil, "wwew", nil,3} -- #t1 = 6
local t2 = { 1,2, nil, "wwew", 3, nil }  -- #t2 = 2
What's wrong with all of that? Why tables with a nil as the last value are truncated to the first nil occurrence while with any non-nil end are counted correct?
  Reply With Quote
01-28-12, 04:52 AM   #7
Xubera
A Cobalt Mageweaver
 
Xubera's Avatar
AddOn Author - Click to view addons
Join Date: May 2009
Posts: 207
Why not try it a different way.
Code:
local q={GetGossipAvailableQuests()}
for i=1, GetNumGossipAvailableQuests() do
    print(1 + ((i-1)*5), q[1 + ((i-1)*5)])
end
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » How to obtain vale count from table?


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