Page 1 of 1

Embedding a For Loop inside another For Loop

PostPosted: Tue Sep 19, 2017 5:49 pm
by chrisr
Hello All!! I have what is a probably a really easy question for some of you. Though I am struggling a bit with the syntax. I have some Custom script buttons, lets say addressed 1001 to 1032. I want to label these buttons IN 1 to IN 32. So this is what I've tried. No luck yet, but still working it.
Code: Select all
For i = 1001 to 1032
{
For x = 1 to 32
{
WDCustomLabel(i,"IN" +x)
}
}


AM I way way off? or getting close. I've tried a couple other ways to do this, this code above labels all the buttons IN 32. I've also tried the following, I moved both For Loops outside the brackets.

Code: Select all
For i = 1001 to 1032
For x = 1 to 32
{
WDCustomLabel(i,"IN" +x)
}


That doesnt run at all. I guess my question is why does the first script only return 32 for each label.

Thanks again for any help. This one is boggling me a bit. Nesting a for loop has come up a couple times for me. I've never been able to get something like this to work. So I've just done the good ole right click and changed it. Just looking to speed up this process :)

Have a great week everyone!!

Cheers,

Chris

Re: Embedding a For Loop inside another For Loop

PostPosted: Wed Sep 20, 2017 8:43 pm
by justyn roy
Hi Chris,

What you've probably noticed with your script is that it labels all of the buttons to 32, is that correct?


What it's doing is going to each button ( for i) and running the second nested loop 32 times, then going to the next button and so on.


What you want to do for this is a single loop with some math:

Code: Select all
for i = 1001 to 1032 {
WDCustomScriptLabel(i,"ID " + i - 1000)
}


This runs your loop i 32 times only, not 32 x 32 like in your example.
You then take the i and subtract 1000 from it to get your desired number.

Don't forget the space inside of the quotes with the "IN" - without the space the IN and the number will be right beside each other.


I hope this helps!

Justyn

Re: Embedding a For Loop inside another For Loop

PostPosted: Thu Sep 21, 2017 10:56 am
by Thomas Mrozek
Hi,
your for loop code is working. If you try a different command both both iteration literals are processed in the second loop.
So there should be something wrong with your button IDs maybe.
cheers

Re: Embedding a For Loop inside another For Loop

PostPosted: Thu Sep 21, 2017 7:53 pm
by chrisr
Justyn / Thomas.

Thanks!! Justyn I did end up using the math. Its a good way to go. I was just curious if you could call out two separate For Loops with different i / x and as along as there was the same count, if indeed they would work. the Math behind the forloop is a great way to do this.

Thomas, I def couldnt get it working. I'll try again if you said it works, then there is something wrong on my end. Thanks for testing!!

Have a great day.

Re: Embedding a For Loop inside another For Loop

PostPosted: Fri Sep 22, 2017 3:13 pm
by justyn roy
No worries Chris,


As an example - just to visualize what's going on, why don't you try this script:

Code: Select all
for i = 1 to 10 {
   for x =  1 to 50 {
      DebugMessage("i " + i + " ,x " + x)
   }
}   


You'll see in here that you'll get 500 entries into the Debugging window.

The reason this didn't work for your buttons is that for each step of 'i' your 'x' variable counts all the way to the end - in this case 50 - before going on to the next step.

In the debugging window you'll see each step and how it counts up.


Does this help illustrate how this nesting works?

Re: Embedding a For Loop inside another For Loop

PostPosted: Wed Sep 27, 2017 4:00 pm
by chrisr
Justyn, Thanks yes. This is really great. Thank you so much for helping me understand how this plays out.

Much appreciated!!

Chris