WoW:API select: Difference between revisions

758 bytes added ,  15 August 2023
m
Move page script moved page API select to WoW:API select without leaving a redirect
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...)
 
m (Move page script moved page API select to WoW:API select without leaving a redirect)
 
(6 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{tocright}}
{{wowlua}}
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)


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


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


== 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.
local msg
  for i = 1, select('#', ...) do
  for i = 1, select('#', ...) do
     DEFAULT_CHAT_FRAME:AddMessage(select(i, ...))
     msg = select(i, ...)
    DEFAULT_CHAT_FRAME:AddMessage(msg)
  end
  end


Line 25: Line 30:
  -- b = 'c'
  -- b = 'c'
  -- c = nil
  -- c = nil
=== Catenation ===
select can be used to catenate a vararg list into an array. This is useful for returning info from [[API_GetBinding | 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 ==
== Notes ==
Line 35: Line 53:
: <pre>select(-1, 1, 2, 3, 4, 5) -- returns 5</pre>
: <pre>select(-1, 1, 2, 3, 4, 5) -- returns 5</pre>
* A decimal index will be rounded to the nearest integer (1.1 rounds to 1, 1.9 rounds to 2).
* 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.
<pre>-- these are identical
-- number 1
local msg, r, g, b, a, id = select(1, ...)
DEFAULT_CHAT_FRAME:AddMessage(msg, r, g, b, a, id)


== Disclaimer ==
-- number 2
* Tested using the command line. Lua v5.1.2. Not tested in the WoW environment.
DEFAULT_CHAT_FRAME:AddMessage(select(1, ...))</pre>
 
{{LUA}}
Anonymous user