Embedding a For Loop inside another For Loop

Christie Widget Designer Version 6 - Q&A

Moderator: Moderator Group

Forum rules
When asking make sure to include the operating system and the revision of Widget Designer you are using.

Embedding a For Loop inside another For Loop

Postby chrisr » Tue Sep 19, 2017 5:49 pm

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
Thank You

ChrisR
SurfacePro3 - 2GHz i5 - 4GB ram - Windows 10 Pro - WD Pro 4.7 Rev2500 and WD v6
Sager Laptop - Intel 2.4GHz i7 3630QM - 16GB ram - Windows 7 Pro Svc Pack1 - Offline Manager v6
chrisr
 
Posts: 143
Joined: Tue Jan 26, 2016 10:50 pm

Re: Embedding a For Loop inside another For Loop

Postby justyn roy » Wed Sep 20, 2017 8:43 pm

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
Justyn Roy

Online Manual
User avatar
justyn roy
 
Posts: 132
Joined: Tue Sep 27, 2016 1:26 am

Re: Embedding a For Loop inside another For Loop

Postby Thomas Mrozek » Thu Sep 21, 2017 10:56 am

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
Thomas Mrozek
Product Manager
User avatar
Thomas Mrozek
 
Posts: 138
Joined: Mon Apr 20, 2009 11:08 am

Re: Embedding a For Loop inside another For Loop

Postby chrisr » Thu Sep 21, 2017 7:53 pm

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.
Thank You

ChrisR
SurfacePro3 - 2GHz i5 - 4GB ram - Windows 10 Pro - WD Pro 4.7 Rev2500 and WD v6
Sager Laptop - Intel 2.4GHz i7 3630QM - 16GB ram - Windows 7 Pro Svc Pack1 - Offline Manager v6
chrisr
 
Posts: 143
Joined: Tue Jan 26, 2016 10:50 pm

Re: Embedding a For Loop inside another For Loop

Postby justyn roy » Fri Sep 22, 2017 3:13 pm

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?
Justyn Roy

Online Manual
User avatar
justyn roy
 
Posts: 132
Joined: Tue Sep 27, 2016 1:26 am

Re: Embedding a For Loop inside another For Loop

Postby chrisr » Wed Sep 27, 2017 4:00 pm

Justyn, Thanks yes. This is really great. Thank you so much for helping me understand how this plays out.

Much appreciated!!

Chris
Thank You

ChrisR
SurfacePro3 - 2GHz i5 - 4GB ram - Windows 10 Pro - WD Pro 4.7 Rev2500 and WD v6
Sager Laptop - Intel 2.4GHz i7 3630QM - 16GB ram - Windows 7 Pro Svc Pack1 - Offline Manager v6
chrisr
 
Posts: 143
Joined: Tue Jan 26, 2016 10:50 pm


Return to Widget Designer V6

Who is online

Users browsing this forum: No registered users and 26 guests