Can this be done on AHK (for)?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
bapl
Posts: 119
Joined: 17 Apr 2021, 00:24

Can this be done on AHK (for)?

Post by bapl » 27 Sep 2022, 02:44

Can this be done on AHK?

Code: Select all

for (int i = 120; i <= 250; i++)
For example, do something like this, but without entering the entire sequence.

Code: Select all

for k, i in [120, 250]
Of course, I can enter from 120 to 250, but this does not suit me.

"for" seems to be faster and works better in a loop than if I did the same through "if".

Rohwedder
Posts: 7616
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Can this be done on AHK (for)?

Post by Rohwedder » 27 Sep 2022, 04:30

Hallo,
I think no! As far as I know, the enumeration inside a For-Loop can only be done from the beginning.
In detail your requirement would have to run like this:

Code: Select all

#Persistent
Array := {}
Loop, 300 ;Creating a test object
	Array[A_Index] := "Value: " A_Index
_enum := Array._NewEnum()
Loop ;Enumbered until 120
	_enum.Next(Key, Value)
Until, Key = 120
While, Key <= 250 ;Enumbered until 250
{
	ToolTip,% "Key: " Key " > " Value
	Sleep, 100
	_enum.Next(Key, Value)
}

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Can this be done on AHK (for)?

Post by swagfag » 27 Sep 2022, 04:53

literally... what???


Descolada
Posts: 1123
Joined: 23 Dec 2021, 02:30

Re: Can this be done on AHK (for)?

Post by Descolada » 27 Sep 2022, 05:13

Since For loop just a fancy While loop, then you could use

Code: Select all

i := 119
While (++i <= 250) {
    ; do stuff
}
Alternatively you could use Coco's Range() function.

User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Can this be done on AHK (for)?

Post by FanaticGuru » 27 Sep 2022, 16:25

bapl wrote:
27 Sep 2022, 02:44
Can this be done on AHK?

Code: Select all

for (int i = 120; i <= 250; i++)
For example, do something like this, but without entering the entire sequence.

Code: Select all

for k, i in [120, 250]
Of course, I can enter from 120 to 250, but this does not suit me.

"for" seems to be faster and works better in a loop than if I did the same through "if".

More for fun than usefulness.

Code: Select all

while Y:=for(4,10,2) ; Begin, End, Step
	MsgBox % Y

while Y:=for(3,9,3) ; Begin, End, Step
	MsgBox % Y

while Y:=for(4,1,-1) ; Begin, End, Step
	MsgBox % Y


for(Begin,End,Step)
{
	static Next, B, E, S, X
	if !Next
	{
		X:=B:=Begin, E:=End, S:=Step, Next:=true
		return X
	}
	if (S>0?((X+=S)>E):((X+=S)<E))
		return Next:=false
	else
		return X
}

This is by no means faster than any manual code. About the only advantage is maybe readability and ease of implementation.

It has also been tested for about 60 seconds, so I would not use it for anything too critical.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Can this be done on AHK (for)?

Post by malcev » 28 Sep 2022, 04:57

You have bug here

Code: Select all

while Y:=for(0,10,2) ; Begin, End, Step
	MsgBox % Y
With i >=0 You can try such monster (just for funny) :)

Code: Select all

Loop % ((i:=120)-250)*++i/-i+--i-(--i)
   MsgBox % ++i
Or this one:

Code: Select all

Loop % ((i:=120)-250)*++i/-i+i---i--
   MsgBox % ++i

AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Can this be done on AHK (for)?

Post by AHKStudent » 28 Sep 2022, 08:39

malcev wrote:
28 Sep 2022, 04:57
You have bug here

Code: Select all

while Y:=for(0,10,2) ; Begin, End, Step
	MsgBox % Y
With i >=0 You can try such monster (just for funny) :)

Code: Select all

Loop % ((i:=120)-250)*++i/-i+--i-(--i)
   MsgBox % ++i
Or this one:

Code: Select all

Loop % ((i:=120)-250)*++i/-i+i---i--
   MsgBox % ++i
😂😂😂😂😂😂😂😂😂😂

Post Reply

Return to “Ask for Help (v1)”