Page 1 of 1

For each cycle

Posted: 26 Apr 2018, 14:36
by jekko1976
Wouldn't be cool to add the for each cycle, like php, to our beloved autohotkey?

Bye

Re: For each cycle

Posted: 26 Apr 2018, 17:07
by swagfag
what do u mean? there is a for each already

Re: For each cycle

Posted: 27 Apr 2018, 02:19
by jekko1976
i'm not sure about that
For sure there are almost 35 way different to obtain a Foreach cycle but, AFAIK the literal Foreach doesn't exist.

Code: Select all

Array := []
Array[1] :=how
Array[2] :=are
Array[3] :=you
For each element in Array
	{
		MsgBox, element
	}
this code does not compile.

Anyway it's not necessary, of course. It could be useful for who came by php, nothing more.

Google told me that the case has been discussed many years ago
https://autohotkey.com/boards/viewtopic.php?t=4259

Re: For each cycle

Posted: 27 Apr 2018, 02:46
by nnnik

Code: Select all

Array := []
Array[1] :=how
Array[2] :=are
Array[3] :=you
For each, element in Array
	{
		MsgBox % element
	}

Re: For each cycle

Posted: 27 Apr 2018, 04:00
by just me

Code: Select all

Array := []
Array[1] := "how" ; if it shall be a literal string
Array[2] := "are"
Array[3] := "you"
For each, element in Array
	{
		MsgBox %element% ; if it shall be a variable reference
	}

Re: For each cycle

Posted: 27 Apr 2018, 04:37
by jekko1976
ah, with the comma it works.
Ok, thank you very much

Re: For each cycle

Posted: 01 May 2018, 10:13
by guest3456
jekko1976 wrote:ah, with the comma it works.
Ok, thank you very much
realize its just a trick, the syntax is for key,val in object, you are just giving the 'key' variable the name 'each' since you don't care about the 1,2,3 indexes in your array. you can name them whatever you want

https://autohotkey.com/docs/commands/For.htm

Code: Select all

Array := []
Array[1] := "how"
Array[2] := "are"
Array[3] := "you"
For each, element in Array
	{
		MsgBox %each% : %element%
	}

Code: Select all

Array := []
Array[1] := "how"
Array[2] := "are"
Array[3] := "you"
For foo, bar in Array
	{
		MsgBox %foo% : %bar%
	}