Page 1 of 1

Trying to use for loop to structure redundant data

Posted: 02 Jun 2020, 11:38
by gravanoc
I'm a bit confused by the for loop syntax used in AHK, and maybe what I'm thinking of is not available, or I'm just unused to the syntax.

My goal is to assist in iterating over the same two alternating commands about 50 times.

Code: Select all

sleep, 25
Send i
Ideally, I could use some kind of loop to just repeat that behavior, without simply copying and pasting 50 times. Maybe the for ... in loop isn't geared towards this issue? Thank you.

Re: Trying to use for loop to structure redundant data

Posted: 02 Jun 2020, 12:15
by swagfag

Code: Select all

Loop 50
{
	sleep, 25
	Send i
}
for is for looping through collections, like arrays, or calling enumerators

Re: Trying to use for loop to structure redundant data

Posted: 02 Jun 2020, 13:32
by gravanoc
Thank you!

Re: Trying to use for loop to structure redundant data

Posted: 02 Jun 2020, 18:48
by yoman_81
so in the same way:

Code: Select all


random, diceroll, 1, 30
; we need this later
remainder := 30 - %diceroll%

; send tabs for the amount of diceroll result
Loop %diceroll% 
{
sleep 25
Send {Tab}
}
; send only ONCE after a random amount of tabs
Send {Space}
; now send the remaining tabs up to the amount of "max" 
Loop  %remainder%
 {
sleep 25
Send {Tab}
 } 
should work too?
what I wanted to do;
- roll between 1,30 , result is the var "diceroll"
- Run a command times the amount of the diceroll
- Run a different command, ONLY ONCE
- Run a command times %remainder%. %remainder% is "maximum roll minus diceroll result"

for later: the maximum for the roll needs to be derived from yet another parameter and not be static. (as in random, diceroll, 1, %MAXVAR%)

any obvious syntax errors?

Re: Trying to use for loop to structure redundant data

Posted: 02 Jun 2020, 19:34
by Chunjee
You got some legacy syntax in there and questionable indentation.

I think this should help:

Code: Select all

random, diceroll, 1, 30
; we need this later
remainder := 30 - diceroll

; send tabs for the amount of diceroll result
Loop, % diceroll {
	sleep 25
	Send {Tab}
}
; send only ONCE after a random amount of tabs
Send {Space}
; now send the remaining tabs up to the amount of "max" 
Loop % remainder {
	sleep 25
	Send {Tab}
}