WoW:API for: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
(Added "for, in" syntax)
(Change category from "LUA Functions" to "Lua functions".)
Line 1: Line 1:
{{UI-article}}
Native Lua statement:


A "for" loop.
for <variable> = <lowvalue>, <highvalue>,<step> do <statements>; end
 
'''for <variable> = <lowvalue>, <highvalue>,<step> do <statements>; end'''


Executes 'statements' for each value of 'variable' from 'lowvalue' to 'highvalue', at a step value of 'step' inclusive.  Other syntax options yet unknown.
Executes 'statements' for each value of 'variable' from 'lowvalue' to 'highvalue', at a step value of 'step' inclusive.  Other syntax options yet unknown.
Line 44: Line 42:


[[User:Tigerheart|Tigerheart]] 03:34, 3 Nov 2005 (EST) - Added info on <step>
[[User:Tigerheart|Tigerheart]] 03:34, 3 Nov 2005 (EST) - Added info on <step>
[[User:Tigerheart|Tigerheart]] 15:10, 7 Feb 2006 (EST) - Added "for, in" syntax


[[User:Tigerheart|Tigerheart]] 15:10, 7 Feb 2006 (EST) - Added "for, in" syntax
{{LUA}}

Revision as of 12:14, 26 May 2006

Native Lua statement:

for <variable> = <lowvalue>, <highvalue>,<step> do <statements>; end

Executes 'statements' for each value of 'variable' from 'lowvalue' to 'highvalue', at a step value of 'step' inclusive. Other syntax options yet unknown.

for <variable>,<integer> in <array> do <statements>;end

Executes 'statements' for each 'variable' with index 'integer' in 'array'.


Examples:

 /script for i=1,10 do DEFAULT_CHAT_FRAME:AddMessage(format("%d",i));end

More interesting example: (remember, all one line)

 /script for i=1,5 do
   name,description,standingID,barValue,atWarWith,canToggleAtWar,isHeader,isCollapsed=GetFactionInfo(i);
   DEFAULT_CHAT_FRAME:AddMessage(format("%s %d/%d,name,barValue*21000,21000));
 end


A few examples using the newly found <step> value:

/script for i=10,1,-1 do DEFAULT_CHAT_FRAME:AddMessage(format("%d",i));end

Will count from 10 to 1.

/script for i=10,0,-2 do DEFAULT_CHAT_FRAME:AddMessage(format("%d",i));end

Will count from 10 to 0 skipping every other number.

/script for i=2,10,2 do DEFAULT_CHAT_FRAME:AddMessage(format("%d",i));end

Will count even numbers from 2 to 10.

Examples for the 'in' version of the syntax:

/script myTable = {"a","b","c","d"};
for var,i in myTable do
     DEFAULT_CHAT_FRAME:AddMessage("The value of index "..i.." is "..var);
end;

Output:

The value of index 1 is a
The value of index 2 is b
The value of index 3 is c
The value of index 4 is d


Tigerheart 03:34, 3 Nov 2005 (EST) - Added info on <step> Tigerheart 15:10, 7 Feb 2006 (EST) - Added "for, in" syntax

Template:LUA