For-Loop Examples

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

For-Loop Examples

18 Sep 2018, 00:32

Autohotkey's implementation of For-Loops look weird and don't ever use them as the other type of loops have worked out very well. However, want to have a better understanding of Autohotkey For-Loops.

Can people give small working example scripts of For-Loops (with comments on the code) to show how they work or how they use them? Greatly appreciative.
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: For-Loop Examples

18 Sep 2018, 08:17

AHKStudent wrote:https://autohotkey.com/docs/misc/Arrays.htm great code comments
Wasn't referring to just loops or arrays, but specifically For-Loops. https://autohotkey.com/docs/commands/For.htm

Did an extensive search, and did happen to find a good For-Loop with heavily commented code that shows usage. Was hoping for more like this, where people could show usage and code comments.

By TheDewd from- https://autohotkey.com/boards/viewtopic ... +k#p226418

Code: Select all

#SingleInstance, Force ; Only allow one running instance of script. If you attempt to run the script while it's already running, it will replace the current instance with the new instance rather than having 2 running instances at one time.

MyArray := ["MMEDIA32", "MMEDIA01", "MMEDIA05", "MMEDIA32", "MMEDIA32", "MMEDIA05"] ; The array

List1 := "" ; Initialize empty variable to store data to at a later time
List2 := "" ; Initialize empty variable to store data to at a later time

; K & V can be named anything. They are essentially variables to store data to.
; K being Key, and V being Value.
; K is the index, or the number of interations the loop has been executed
For K, V In MyArray {
 List1 .= V (K = MyArray.MaxIndex() ? "" : "`n") ; Write each value from array to string / also using ternary operation to specify whether to add NewLine or not, because we don't want a NewLine added at the end of the list
 List2 .= V (K = MyArray.MaxIndex() ? "" : "`n") ; Write each value from array to string
}

Sort, List1, U ; Sort first string list removing duplicates -- only unique values. We want the second list string to contain all duplicates

StrReplace(List1, "`n", "`n", LineCount) ; Count how many lines (items) are in List2

; Parse the first list (unique, no duplicates)
Loop, Parse, List1, `n
{
 StrReplace(List2, A_LoopField, A_LoopField, WordCount) ; Count how many times each word from List1 appears in List2
 FinalList .= A_LoopField ": used " WordCount " times" (A_Index = LineCount + 1 ? "" : "`n") ; Write that count to the FinalList variable for each word
}

MsgBox, % FinalList ; Display final list
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: For-Loop Examples

18 Sep 2018, 11:36

SOTE wrote:
AHKStudent wrote:https://autohotkey.com/docs/misc/Arrays.htm great code comments
Wasn't referring to just loops or arrays, but specifically For-Loops. https://autohotkey.com/docs/commands/For.htm
he cited the Arrays page, because for-loops are only used if you are working with objects/arrays. that is all they are used for.

User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: For-Loop Examples

18 Sep 2018, 12:29

- I have a section, LOOPING THROUGH AN OBJECT'S KEYS AND VALUES, here:
jeeswg's objects tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=29232
- I've also just added a section called FOR LOOP EXAMPLES. Cheers.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: For-Loop Examples

19 Sep 2018, 02:52

he cited the Arrays page, because for-loops are only used if you are working with objects/arrays. that is all they are used for.
- I have a section, LOOPING THROUGH AN OBJECT'S KEYS AND VALUES, here:
jeeswg's objects tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=29232
- I've also just added a section called FOR LOOP EXAMPLES. Cheers.
Thanks Jeeswgs and guest.

As far as a tutorial, I thought that Joe Glines also did a good job, where he goes over arrays and for-loops on a basic level. https://youtu.be/tY7IQ_t_Gek

Have used loops, arrays, and pseudo-arrays in previous scripts, but have done so without for-loops, so wanted to become more familiar with them. I understand them on a basic level (as shown below), but was curious about how more advanced programmers use for-loops to solve different problems. Was hoping for various examples of the kind shown by TheDewd (previous post), with comments.

Code: Select all

Text := ["Text1", "Text2", "Text3", "Text4"] 
for key, value in Text 
List .= value "`n"
MsgBox % List

Code: Select all

colours := Object("red", 0xFF0000, "blue", 0x0000FF, "green", 0x00FF00)
; The above expression could be used directly in place of "colours" below:
for key, value in colours
List .= key "=" Value "`n"
MsgBox % List

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: alawsareps, Chunjee, Joey5, mebelantikjaya, mikeyww and 322 guests