WoW:API select: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
m (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...)
 
No edit summary
Line 1: Line 1:
{{tocright}}
{{tocright}}
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).
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)
  local ret1, ret2, retN = select(index, list)


Line 6: Line 6:
=== Parameters ===
=== Parameters ===
:;index : Any non-zero number or the string "#".
:;index : Any non-zero number or the string "#".
:;list : Usually an ellipse (...).
:;list : Usually an ellipsis (...).


=== Returns ===
=== Returns ===
Line 13: Line 13:
== Examples ==
== Examples ==
  -- Common usage.
  -- Common usage.
  local num = select('#', ...) -- Returns the number of arguments in the ellipse.
  local num = select('#', ...) -- Returns the number of arguments in the ellipsis.
  local arg = select(i, ...) -- Returns the value at index '''i'''.
  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.
  -- Print all of the function's arguments (those passed to the ellipsis (...) only) to the default chat frame.
  for i = 1, select('#', ...) do
  for i = 1, select('#', ...) do
     DEFAULT_CHAT_FRAME:AddMessage(select(i, ...))
     DEFAULT_CHAT_FRAME:AddMessage(select(i, ...))

Revision as of 07:51, 17 February 2008

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

Parameters

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

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 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.
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