Thread Tools Display Modes
10-21-09, 11:10 AM   #1
Shadowfaux
A Defias Bandit
 
Shadowfaux's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 2
function with nested FOR's?

Hey, i was wondering if someone could clarify a niggle for me.

If i have a function with 2 nested For loops, with 2 nested if-elseif statements within, would the program still run correctly (assuming the For's, if s and arrays were referencing seperate variables)?

From an english writing POV it would work well assuming that there was no way to call up either of the For's before they were needed.

pseudosyntax looks like this:
Code:
 #
local function XYZ(arg1-3,...)
          for k,v in pairs (array1) do
               if strfind (arg1, v) then
                   --perform stuff
               elseif
                  --otherStuff and
                   for k,v in pairs (array2) do
                     if strfind(arg1,v) do 
                              diff stuff from before
                        elseif
                               even more diff
  **Ends in all the right places**
Would this be an inefficient but working method? or would it fail from the start?
__________________
"Be yourself, everyone else is taken"
  Reply With Quote
10-21-09, 11:57 AM   #2
Katae
A Cobalt Mageweaver
AddOn Author - Click to view addons
Join Date: Jun 2007
Posts: 208
Originally Posted by Shadowfaux View Post
Code:
 #
local function XYZ(arg1-3,...)
          for k,v in pairs(array1) do
               if strfind(arg1, v) then
                   --perform stuff
               elseif true then
                  --otherStuff and
                   for k,v in pairs(array2) do
                     if strfind(arg1,v) then
                              diff stuff from before
                        elseif true then
                               even more diff
  **Ends in all the right places**
Fixed, looks fine though.

Last edited by Katae : 10-21-09 at 12:59 PM.
  Reply With Quote
10-21-09, 01:08 PM   #3
xConStruct
A Chromatic Dragonspawn
 
xConStruct's Avatar
AddOn Author - Click to view addons
Join Date: May 2008
Posts: 199
Yep, it should work fine - the inner for-loop and if-blocks only get called when the containing block is executed. So when the block's condition is false, the contained code/loop won't get called.

But as you suggested it could be very inefficient.
In your example, the inner for-loop would be executed again for each iteration of the outer one, although the results would be the same, because the inner for-loop doesn't make use of any of the variables of the outer one. It is best to avoid nested loops if possible. But in the end it really depends on what you want to do in your "stuff" sections.
__________________
« Website | GitHub »

Oh hai!
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » function with nested FOR's?


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off