WoW:API pairs: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
(fixed bug in in the example ("local random_array"..."pairs(fruits)"))
m (Changed layout slightly, corrected spelling)
Line 1: Line 1:
Native Lua statement:
Native Lua statement:


Used in loop constructs to iterate through the values of a table, with index beeing anything.
Used in loop constructs to iterate through the values of a table, with index being anything.


Example:
Example:


  local random_array = { mug = "coffee", [42] = "universe", true = "false" }
  local random_array = { mug = "coffee", [42] = "universe", true = "false" }
 
  for index,value in pairs(random_array) do  
  for index,value in pairs(random_array) do  
   if (type(index) == "number") then
   if (type(index) == "number") then

Revision as of 02:35, 3 November 2008

Native Lua statement:

Used in loop constructs to iterate through the values of a table, with index being anything.

Example:

local random_array = { mug = "coffee", [42] = "universe", true = "false" }

for index,value in pairs(random_array) do 
  if (type(index) == "number") then
     index = tostring(index)
  elseif (type(index) == "boolean") then
    if (index) then
      index = "true"
    else
      index = "false"
    end
  end
  DEFAULT_CHAT_FRAME:AddMessage(index.." : "..value)
end

Result:

 mug : coffee
 42 : universe
 true : false

would be output to the chat window.

Template:LUA