Foreach loop for AutoHotkey v2.

Discuss the future of the AutoHotkey language
vasili111
Posts: 747
Joined: 21 Jan 2014, 02:04
Location: Georgia

Foreach loop for AutoHotkey v2.

15 Aug 2014, 10:16

Currently AutoHotkey has several loops that in fact are functioning as Foreach loop. So AutoHotkey has these loops that are functioning like Foreach loop:

1. For - is used for arrays and objects.
2. LoopReg or Loop, Reg - for registry.
3. LoopFiles or Loop, Files - for files and folders.
4. Loop, read - for each lines in text file.
5. LoopParse or Loop, Parse - for substrings (fields) from a string.

Each of above 5 commands are in fact Foreach loops but they have different names. So 5 different commands that in fact are functioning as one Foreach command. I think it is misleading.
In some cases we use Loop loop as Foreach loop. Autohotkey also uses Loop as While loop. So we are using one loop as two kind of loops and it is misleading.
In some cases we use For loops as Foreach loop. It is also misleading because many programming languages are using For loop as For loop (JavaScript, C++).

I propose to use only Foreach loop instead of all these loops. So, it will look like:

1. Foreach, key - is used for arrays and objects.
2. Foreach, subkey - for registry.
3. Foreach, ff - for files and folders.
4. Foreach, line - for each lines in text file.
5. Foreach, substring - for substrings (fields) from a string.

I think it will be more intuitive and right way to use one Foreach loop instead of all different loops that are currently functioning as Foreach loop.
Last edited by vasili111 on 15 Aug 2014, 12:35, edited 1 time in total.
DRAKON-AutoHotkey: Visual programming for AutoHotkey.
User avatar
nnnik
Posts: 3981
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Foreach loop for AutoHotkey v2.

15 Aug 2014, 10:53

Or rather:

Code: Select all

For each,RegestryKey in GetRegKeys()
For each,File in GetFiles(Pattern)
For each,Line in StringSplit(InputFile,["`r","`n","`r`n"])
For each, Str in StringSplit(InputString,delimiters)
Recommends AHK Studio
vasili111
Posts: 747
Joined: 21 Jan 2014, 02:04
Location: Georgia

Re: Foreach loop for AutoHotkey v2.

15 Aug 2014, 11:02

nnnik
In AutoHotkey as far as I know all commands consist of one word (except else if) and I think we need to stick to that rule. So I think Foreach is better or it can be written as ForEach.
DRAKON-AutoHotkey: Visual programming for AutoHotkey.
User avatar
nnnik
Posts: 3981
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Foreach loop for AutoHotkey v2.

15 Aug 2014, 11:28

Well you dont have to write For each.
You can write For FileName,File in FileObjlist.
Recommends AHK Studio
vasili111
Posts: 747
Joined: 21 Jan 2014, 02:04
Location: Georgia

Re: Foreach loop for AutoHotkey v2.

15 Aug 2014, 11:34

nnnik wrote: You can write For FileName,File in FileObjlist.
Please, write exactly what you mean should be written. You mean using For instead of Foreach?
DRAKON-AutoHotkey: Visual programming for AutoHotkey.
toralf
Posts: 533
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: Foreach loop for AutoHotkey v2.

15 Aug 2014, 12:14

That's what i understand. Basically "each" becomes then the same as "A_Index"
ciao
toralf
User avatar
fincs
Posts: 500
Joined: 30 Sep 2013, 14:17
GitHub: fincs
Location: Seville, Spain
Contact:

Re: Foreach loop for AutoHotkey v2.

15 Aug 2014, 12:54

Now that we've opened this can of worms, here's my suggestion:

Code: Select all

for file in Files("Path" [, "F|D|R"]) ; file is an object (with dynamically calculated properties like RegExMatch objects): file.fullPath, file.extension, etc etc
for key in RegKeys("RootKey\Key" [, "V|K|R"]) ; as above. e.g. key.subKey, key.type, etc etc
for field in StrFields(string [, delimiters, omitChars]) ; field is a string
for line in FileLines(fileName) ; line is a string
Renaming 'for' to 'foreach' is IMO extremely pointless and more verbose.
fincs
Windows 10 x64 Build 17134 / AutoHotkey v1.1.29.01
Get SciTE4AutoHotkey v3.0.06.01 - [My project list]
vasili111
Posts: 747
Joined: 21 Jan 2014, 02:04
Location: Georgia

Re: Foreach loop for AutoHotkey v2.

15 Aug 2014, 13:05

toralf wrote:That's what i understand. Basically "each" becomes then the same as "A_Index"
No. Currently we have For , LoopReg or Loop, Reg , LoopFiles or Loop, Files , Loop, read , LoopParse or Loop, Parse and I suggested to make instead of all these loops one Foreach loop.
DRAKON-AutoHotkey: Visual programming for AutoHotkey.
guest3456
Posts: 2496
Joined: 09 Oct 2013, 10:31

Re: Foreach loop for AutoHotkey v2.

15 Aug 2014, 13:22

Can of worms indeed. Why is this needed?

Simply because some of the Loop constructs can be 'thought of' as "foreach"? Every for/while/loop structure is always going to be a "foreach". Thats what loops do. They iterate over items.

Even a normal loop like this could be considered a "foreach":

Code: Select all

Loop, 10
   msgbox, loop number %a_index%
"Foreach loop#, display message box showing loop #"

But the word "Loop" is far more intuitive for almost every person compared to "for" or "foreach". Anyway, if the sole purpose of this change is a subjective account of what is more "intuitive", seems like its wholly unnecessary

AHKv2 aims to break compatibility, but soon you will be requesting to change everything so much that it will no longer even look like AHK. This is a waste of dev time
Last edited by guest3456 on 15 Aug 2014, 13:34, edited 1 time in total.

toralf
Posts: 533
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: Foreach loop for AutoHotkey v2.

15 Aug 2014, 13:34

Fincs, what would be the difference between your strfields() and strsplit()?

I assume filelines() returns basically an array of lines created by FileReadLine, right?

But I agree with guest3456. Unless there is an additional benefit which currently do not see
ciao
toralf
User avatar
fincs
Posts: 500
Joined: 30 Sep 2013, 14:17
GitHub: fincs
Location: Seville, Spain
Contact:

Re: Foreach loop for AutoHotkey v2.

15 Aug 2014, 13:47

None of the functions I proposed in my earlier post would pre-calculate the entries that would be iterated over, instead the enumerator would compute them on demand.
fincs
Windows 10 x64 Build 17134 / AutoHotkey v1.1.29.01
Get SciTE4AutoHotkey v3.0.06.01 - [My project list]
guest3456
Posts: 2496
Joined: 09 Oct 2013, 10:31

Re: Foreach loop for AutoHotkey v2.

15 Aug 2014, 14:16

vasili111 wrote:
nnnik wrote:
vasili111 wrote:nnnik
In AutoHotkey as far as I know all commands consist of one word (except else if) and I think we need to stick to that rule. So I think Foreach is better or it can be written as ForEach.
Well you dont have to write For each.
You can write For FileName,File in FileObjlist.
Please, write exactly what you mean should be written. You mean using For instead of Foreach?
You don't understand.
vasili111 wrote:
toralf wrote:That's what i understand. Basically "each" becomes then the same as "A_Index"
No. Currently we have For , LoopReg or Loop, Reg , LoopFiles or Loop, Files , Loop, read , LoopParse or Loop, Parse and I suggested to make instead of all these loops one Foreach loop.
And he is saying, why make all of these into "foreach" when they can already be done with "for"?

What you are proposing is already possible using the current syntax by simply naming your variables, or creating your own functions to return an object to use in the for loop.

This is working AHK v1.1 code:

Loop:

Code: Select all

Loop, 5
   msgbox, loop number %A_Index%
   
for each in [1,2,3,4,5]
   msgbox, for iteration %each%
Loop, Parse

Code: Select all

string := "red,blue,green"

Loop, Parse, string, `,
   msgbox, % "loop parse: " . A_loopField

for each, field in StrSplit(string, ",")
   msgbox, % "for: " . field
Loop, Files

Code: Select all

Loop, %A_ScriptDir%\*.*
   msgbox, % "loop files:`n`n" . A_LoopFileName

for each, file in GetFiles(A_ScriptDir)
   msgbox, % "for:`n`n" . file
   
GetFiles(dir)
{
   array := []
   Loop, %dir%\*.*
      array.Insert(A_LoopFileName)
   return array
}
You will note that using the variable name "each" for the object keys is completely arbitrary and irrelevant in these exmaples, but simply done because you like the sound of it

lexikos
Posts: 6212
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Foreach loop for AutoHotkey v2.

15 Aug 2014, 18:04

vasili111 wrote:Each of above 5 commands are in fact Foreach loops but they have different names. So 5 different commands that in fact are functioning as one Foreach command. I think it is misleading.
I laughed out loud when I read this. Please explain to me how this "misleads" users and how that actually matters.

For/foreach is really just a conditional jump combined with a few method calls:

Code: Select all

_enum := (Expression)._NewEnum()
if IsObject(_enum)
    while _enum.Next(Key, Value)
    {
        ...
    }
(Straight from the manual.) I think it is "misleading" and therefore propose that we do away with for loops and just use if and while. Or perhaps we should do away with while and just use if and goto.

Does anyone else think that "Loop" is easier for beginners to grasp?
Autohotkey also uses Loop as While loop. So we are using one loop as two kind of loops and it is misleading.
Does not. There's a separate command for While loops. Loop Count is equivalent to for (A_Index = 1; A_Index <= Count; ++A_Index), not foreach. AutoHotkey uses "Loop" for several types of loops, because they are loops. Perhaps it should use "Loop" for for-loops and while-loops as well (and I think IronAHK went that way), but I thought it was too verbose.
In some cases we use For loops as Foreach loop. It is also misleading because many programming languages are using For loop as For loop (JavaScript, C++).
AutoHotkey's for loop is modelled after JavaScript's for..in loop. How is it misleading when they have the same name and virtually the same behaviour? AutoHotkey's for loop is even compatible with JavaScript objects.

The Loop commands are not functioning as foreach loops. There is no collection object being enumerated, no enumerator object, and therefore no Next() call on each iteration.
vasili111 wrote:In AutoHotkey as far as I know all commands consist of one word (except else if)
There is no "else if" command or statement; it is two separate statements. But there is "for in", "Loop Parse", "Loop Files", etc. and there was "if x between y and z", "if is", "if in" and "if contains".


guest3456: For once, I agree with you. ;)
User avatar
nnnik
Posts: 3981
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Foreach loop for AutoHotkey v2.

16 Aug 2014, 00:28

@guest3456 exactly what I meant.
Recommends AHK Studio
TWATS

Re: Foreach loop for AutoHotkey v2.

25 Oct 2015, 09:54

What a confusing mess. The documentation needs to explain the pattern of for loops simply.
User avatar
lmstearn
Posts: 178
Joined: 11 Aug 2016, 02:32
GitHub: lmstearn
Contact:

Re: Foreach loop for AutoHotkey v2.

04 Jan 2017, 23:53

Curious that in 11.24.03:

Code: Select all

string := "red,blue,green"
  for each, field in StrSplit(string, ",")
   msgbox, % "for: " . field
compiles the same as

Code: Select all

string := "red,blue,green"
for whateverstringisputinheretheresultisthesame, field in StrSplit(string, ",")
   msgbox, % "for: " . field
Last edited by lmstearn on 05 Jan 2017, 00:09, edited 1 time in total.
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
guest3456
Posts: 2496
Joined: 09 Oct 2013, 10:31

Re: Foreach loop for AutoHotkey v2.

05 Jan 2017, 00:08

lmstearn wrote:Curious that in 11.24.03:

Code: Select all

string := "red,blue,green"
  for each field in StrSplit(string, ",")
   msgbox, % "for: " . field
compiles the same as

Code: Select all

string := "red,blue,green"
for whateverstringisputinheretheresultisthesame, field in StrSplit(string, ",")
   msgbox, % "for: " . field
nothing curious about it:
https://autohotkey.com/docs/commands/For.htm

User avatar
lmstearn
Posts: 178
Joined: 11 Aug 2016, 02:32
GitHub: lmstearn
Contact:

Re: Foreach loop for AutoHotkey v2.

05 Jan 2017, 00:18

Aha:

Code: Select all

for fearofpostingsomethingalittletooobvious, field in StrSplit(string, ",")
   msgbox, %  "fearofpostingsomethingalittletooobvious: " fearofpostingsomethingalittletooobvious " for: " . field
fearofpostingsomethingalittletooobvious is the key!

If key is removed, what happens now?

Code: Select all

string := "red,blue,green"
  
for , field in StrSplit(string, ",")
   msgbox, %  " for: " . field
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
guest3456
Posts: 2496
Joined: 09 Oct 2013, 10:31

Re: Foreach loop for AutoHotkey v2.

05 Jan 2017, 01:08

lmstearn wrote: If key is removed, what happens now?

Code: Select all

string := "red,blue,green"
  
for , field in StrSplit(string, ",")
   msgbox, %  " for: " . field
what do you think happens? did you run your code?

key is not in [brackets] so its a required parameter. its not allowed to be missing. read the help page.
https://autohotkey.com/docs/commands/For.htm

your code is the same as msgbox , param1
command, then optional comma, then first parameter


Return to “AutoHotkey v2 Development”

Who is online

Users browsing this forum: No registered users and 2 guests