Fandom

WoWWiki

API select

104,672pages on
this wiki
Add New Page
Talk2 Share
WoW Lua

Used to traverse a list. This function is usually used to capture the arguments passed to an ellipsis (...). 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 Edit

Parameters Edit

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

Returns Edit

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

Examples Edit

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

Catenation Edit

select can be used to catenate a vararg list into an array. This is useful for returning info from GetBinding, which returns a vararg list of unknown size.

function MyAddon_Catenate(...)
    local t,v
    t = {}
    for i = 1, select("#", ...) do
        v = select(i, ...)
        tinsert(t, v)
    end
    return t
end

Notes Edit

  • 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).
  • Passing a function that returns a list as a parameter to another function will pass each return as a parameter to the function as if you were manually passing each variable to that function, so tinsert(tab, select(1, ...)), will have unpredictable results.
-- these are identical
-- number 1
local msg, r, g, b, a, id = select(1, ...)
DEFAULT_CHAT_FRAME:AddMessage(msg, r, g, b, a, id)

-- number 2
DEFAULT_CHAT_FRAME:AddMessage(select(1, ...))

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Also on Fandom

Random Wiki