View Single Post
05/12/14, 06:46 PM   #8
stjobe
 
stjobe's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 60
Originally Posted by Roupine View Post
You could wrap it in {} to get a table and index it from there:
Lua Code:
  1. local buffInfo = {GetUnitBuffInfo("player",1)}
I could, but that would still leave nine unnecessary table entries.

The correct way (which I just learned and want to share) of getting the last value from GetUnitBuffInfo() is using select():
Lua Code:
  1. local lastValue = select(10, GetUnitBuffInfo("player", 1))
http://www.lua.org/manual/5.1/manual.html:
select (index, ···)
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.
Here's an example showing the difference:
Lua Code:
  1. function manyReturns()
  2.     return "alfa", "beta", "gamma"
  3. end
  4.  
  5. t = {manyReturns()}
  6.  
  7. for k,v in pairs(t) do
  8.     print(k..": "..v)
  9. end
  10.  
  11. v = select("#", manyReturns())
  12. print(v)
  13.  
  14. r = select(v, manyReturns())
  15. print(r)
Which outputs:
Code:
1: alfa
2: beta
3: gamma
3
gamma
Not so messy after all

Last edited by stjobe : 05/12/14 at 06:57 PM. Reason: added example of select
  Reply With Quote