WoW:API select

Revision as of 09:59, 14 January 2008 by WoWWiki>Egingell (New page: {{tocright}} Used to traverse a list. This function usually used to capture the arguments passed to an ellipse (...). local num = select('#', ...) -- Returns the number of arguments in th...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Used to traverse a list. This function is usually used to capture the arguments passed to an ellipse (...). The official usage of this function is to return a list (retN) starting from index to the end of the list (list).

local ret1, ret2, retN = select(index, list)

Arguments

Parameters

index
Any non-zero number or the string "#".
list
Usually an ellipse (...).

Returns

retN
The number of items in the list or every value starting from index to the end of the list.

Examples

-- Common usage.
local num = select('#', ...) -- Returns the number of arguments in the ellipse.
local arg = select(i, ...) -- Returns the value at index i.
-- Print all of the function's arguments (those passed to the ellipse (...) only) to the default chat frame.
for i = 1, select('#', ...) do
    DEFAULT_CHAT_FRAME:AddMessage(select(i, ...))
end
local a, b, c = select(2, 'a', 'b', 'c')
-- a = 'b'
-- b = 'c'
-- c = nil

Notes

  • list can be value1, value2, value3, ... valueN; although, you will never see it used in that manner since you can just access value1 through valueN directly.
  • It is not recommended that you use this method on a table as you would have to pass the table to unpack() for every call to select (once plus the number of consecutive numeric indexes). For tables, use pairs() or ipairs().
  • Indexes beyond the number of list items will return nil.
select(6, 'see', 'dog', 'run') -- returns nil
  • An index of 0 will produce an "index out or range" error.
  • Negative values wrap to the end of the list.
select(-1, 1, 2, 3, 4, 5) -- returns 5
  • A decimal index will be rounded to the nearest integer (1.1 rounds to 1, 1.9 rounds to 2).

Disclaimer

  • Tested using the command line. Lua v5.1.2. Not tested in the WoW environment.

Template:LUA