traditional for loop: for i = a to b (step c) possibilities

Get help with using AutoHotkey and its commands and hotkeys
User avatar
jeeswg
Posts: 4276
Joined: 19 Dec 2016, 01:58
Location: UK

traditional for loop: for i = a to b (step c) possibilities

19 Jan 2018, 03:45

- I have been thinking about traditional for loops, and the best way to achieve them, since I first started using AutoHotkey. It was the one killer feature that Excel macros had, that AutoHotkey did not.
- While thinking about what the very best function/object solution might be (see the links lower down), I considered that using a while loop might be the least-worst solution, of all the solutions available.
- I always felt that Loop was doable/workable, but not quite good enough. The while loop gives a result that I find reasonably satisfactory.
- I find traditional for loops particularly useful for use with offsets with NumGet/NumPut/SubStr, plotting graphs (the values for the axes), and mathematics generally.
- I'm posting here in case anyone else has anything to say re. this.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


Links:

[traditional for loop via functions][for i = a to b (step c)]
Traditional For loop (i.e., step through a sequence) - Suggestions - AutoHotkey Community
https://autohotkey.com/board/topic/71225-traditional-for-loop-ie-step-through-a-sequence/

[traditional for loop via objects][for i = a to b (step c)]
For loop question - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/66916-for-loop-question/#entry423515
enum type and while loop - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/78869-enum-type-and-while-loop/
Helgef
Posts: 2898
Joined: 17 Jul 2016, 01:02
Contact:

Re: traditional for loop: for i = a to b (step c) possibilities

19 Jan 2018, 12:04

Hello jeeswg :wave: .
V2 only,

Code: [Select all] [Download] GeSHi © Codebox Plus

x_to_y_step_k(byref i, x, y, k:=1){
return ( i := x+(a_index-1) ) <= y ? ( a_index += k-1, true ) : ( i -= k, false )
}
; example
while x_to_y_step_k(i, 5, 17, 3)
msgbox(i)

You can do nested loops, you should but use different index.

Cheers.
User avatar
derz00
Posts: 478
Joined: 02 Feb 2016, 17:54
GitHub: derz00
Location: Middle of the round cube

Re: traditional for loop: for i = a to b (step c) possibilities

19 Jan 2018, 12:22

Very nice. Out of curiosity, what about v1 prevents that?
try it and see
...
User avatar
derz00
Posts: 478
Joined: 02 Feb 2016, 17:54
GitHub: derz00
Location: Middle of the round cube

Re: traditional for loop: for i = a to b (step c) possibilities

19 Jan 2018, 12:40

Sure enough. From v2-changes.htm:

The following built-in variables can be assigned values:

A_Index: For counted loops, modifying this affects how many iterations are performed.
try it and see
...
User avatar
KuroiLight
Posts: 324
Joined: 12 Apr 2015, 20:24
GitHub: KuroiLight

Re: traditional for loop: for i = a to b (step c) possibilities

19 Jan 2018, 13:50

you can create a range object and for loop over that:
while a range could be re-used and makes the code look cleaner overall, its likely slower than your while method.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


*edit* I see one of your links has something similar.
Windows 10, Ryzen 1600, 16GB G.Skill DDR4, 8GB RX 480 | [MyScripts][MySublimeSettings] [Unlicense][MIT License]
01/24/18
User avatar
jeeswg
Posts: 4276
Joined: 19 Dec 2016, 01:58
Location: UK

Re: traditional for loop: for i = a to b (step c) possibilities

31 Jan 2018, 01:43

- Cheers KuroiLight, I had forgotten about the Range function approach, until I looked through those links recently, looking for object enumerator examples.
- I generally like the Range function approach, because it's the simplest, but also because it can be useful for contexts outside of loops.
- Some tricky issues are that if there is a mismatch between the start/end values, and the sign of the step, what to do. My function ignores the sign of the step. Another issue is where the start and end values are the same (btw @KuroiLight you should check your function re. this). E.g. Range(1, 1, 1), should probably give an array containing 1.
- Btw my function is not necessarily the most efficient, so I welcome any suggestions/alternatives. Do notify of any issues.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
Capn Odin
Posts: 1288
Joined: 23 Feb 2016, 19:45
Location: Denmark

Re: traditional for loop: for i = a to b (step c) possibilities

31 Jan 2018, 02:42

I have never felt the need for something like this but I think that I would approach it like this

Code: [Select all] [Expand] [Download] (ForLoop.ahk)GeSHi © Codebox Plus

Please excuse my spelling I am dyslexic.
Helgef
Posts: 2898
Joined: 17 Jul 2016, 01:02
Contact:

Re: traditional for loop: for i = a to b (step c) possibilities

31 Jan 2018, 04:14

It is inefficient to calculate the whole range before the loop starts, in case you want to break the loop early, if you want to reuse the range it is fine. Here is another range function, where you specify the number of steps rather than step size, it always hits the end points,

Code: [Select all] [Download] GeSHi © Codebox Plus

eqspace(a,b,n){	; creates an array of n equally spaced numbers between a and b (inclusive).
local
t:=(b-a)/(n-1)
e:=object(), e.SetCapacity(n)
loop n
e.push(a+(A_Index-1)*t)
return e
}

and as a enumerator,

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



More on the topic: How to add to the key of a For-loop

Cheers.

Return to “Ask For Help”

Who is online

Users browsing this forum: Gandalf_yayo, Micromegas, OCP, ouboss and 33 guests