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.
Foreach loop for AutoHotkey v2.
Foreach loop for AutoHotkey v2.
Last edited by vasili111 on 15 Aug 2014, 12:35, edited 1 time in total.
DRAKON-AutoHotkey: Visual programming for AutoHotkey.
Re: Foreach loop for AutoHotkey v2.
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
Re: Foreach loop for AutoHotkey v2.
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.
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.
Re: Foreach loop for AutoHotkey v2.
Well you dont have to write For each.
You can write For FileName,File in FileObjlist.
You can write For FileName,File in FileObjlist.
Recommends AHK Studio
Re: Foreach loop for AutoHotkey v2.
Please, write exactly what you mean should be written. You mean using For instead of Foreach?nnnik wrote: You can write For FileName,File in FileObjlist.
DRAKON-AutoHotkey: Visual programming for AutoHotkey.
Re: Foreach loop for AutoHotkey v2.
That's what i understand. Basically "each" becomes then the same as "A_Index"
ciao
toralf
toralf
Re: Foreach loop for AutoHotkey v2.
Now that we've opened this can of worms, here's my suggestion:
Renaming 'for' to 'foreach' is IMO extremely pointless and more verbose.
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
fincs
Windows 11 Pro (Version 22H2) | AMD Ryzen 7 3700X with 32 GB of RAM | AutoHotkey v2.0.0 + v1.1.36.02
Get SciTE4AutoHotkey v3.1.0 -[My project list]
Windows 11 Pro (Version 22H2) | AMD Ryzen 7 3700X with 32 GB of RAM | AutoHotkey v2.0.0 + v1.1.36.02
Get SciTE4AutoHotkey v3.1.0 -
Re: Foreach loop for AutoHotkey v2.
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.toralf wrote:That's what i understand. Basically "each" becomes then the same as "A_Index"
DRAKON-AutoHotkey: Visual programming for AutoHotkey.
Re: Foreach loop for AutoHotkey v2.
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":
"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
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%
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.
Re: Foreach loop for AutoHotkey v2.
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
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
toralf
Re: Foreach loop for AutoHotkey v2.
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 11 Pro (Version 22H2) | AMD Ryzen 7 3700X with 32 GB of RAM | AutoHotkey v2.0.0 + v1.1.36.02
Get SciTE4AutoHotkey v3.1.0 -[My project list]
Windows 11 Pro (Version 22H2) | AMD Ryzen 7 3700X with 32 GB of RAM | AutoHotkey v2.0.0 + v1.1.36.02
Get SciTE4AutoHotkey v3.1.0 -
Re: Foreach loop for AutoHotkey v2.
You don't understand.vasili111 wrote:Please, write exactly what you mean should be written. You mean using For instead of Foreach?nnnik wrote:Well you dont have to write For each.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.
You can write For FileName,File in FileObjlist.
And he is saying, why make all of these into "foreach" when they can already be done with "for"?vasili111 wrote: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.toralf wrote:That's what i understand. Basically "each" becomes then the same as "A_Index"
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%
Code: Select all
string := "red,blue,green"
Loop, Parse, string, `,
msgbox, % "loop parse: " . A_loopField
for each, field in StrSplit(string, ",")
msgbox, % "for: " . field
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
}
Re: Foreach loop for AutoHotkey v2.
I laughed out loud when I read this. Please explain to me how this "misleads" users and how that actually matters.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.
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)
{
...
}
Does anyone else think that "Loop" is easier for beginners to grasp?
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.Autohotkey also uses Loop as While loop. So we are using one loop as two kind of loops and it is misleading.
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.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++).
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.
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".vasili111 wrote:In AutoHotkey as far as I know all commands consist of one word (except else if)
guest3456: For once, I agree with you.
Re: Foreach loop for AutoHotkey v2.
What a confusing mess. The documentation needs to explain the pattern of for loops simply.
Re: Foreach loop for AutoHotkey v2.
Curious that in 11.24.03:
compiles the same as
Code: Select all
string := "red,blue,green"
for each, field in StrSplit(string, ",")
msgbox, % "for: " . field
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.
itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
Re: Foreach loop for AutoHotkey v2.
nothing curious about it:lmstearn wrote:Curious that in 11.24.03:compiles the same asCode: Select all
string := "red,blue,green" for each field in StrSplit(string, ",") msgbox, % "for: " . field
Code: Select all
string := "red,blue,green" for whateverstringisputinheretheresultisthesame, field in StrSplit(string, ",") msgbox, % "for: " . field
https://autohotkey.com/docs/commands/For.htm
Re: Foreach loop for AutoHotkey v2.
Aha:
fearofpostingsomethingalittletooobvious is the key!
If key is removed, what happens now?
Code: Select all
for fearofpostingsomethingalittletooobvious, field in StrSplit(string, ",")
msgbox, % "fearofpostingsomethingalittletooobvious: " fearofpostingsomethingalittletooobvious " for: " . field
If key is removed, what happens now?
Code: Select all
string := "red,blue,green"
for , field in StrSplit(string, ",")
msgbox, % " for: " . field
itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
Re: Foreach loop for AutoHotkey v2.
what do you think happens? did you run your code?lmstearn wrote: If key is removed, what happens now?Code: Select all
string := "red,blue,green" for , field in StrSplit(string, ",") msgbox, % " for: " . field
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 Development”
Who is online
Users browsing this forum: No registered users and 23 guests