goto becomes loop

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Balduz
Posts: 2
Joined: 29 Feb 2016, 11:42

goto becomes loop

29 Feb 2016, 12:13

Hello everyone!
I introduce myself
my name is Alex

and this is my problem:

Code: Select all

F3 ::

loop 3
{

random, m, 1, 3
goto,% m%

1:
random, k, 1, 2
Send, {a}% k%
Send, {Enter}

2:
random, k, 1, 2
Send, {b}% k%
Send, {Enter}

3:
random, k, 1, 2
Send, {c}% k%
Send, {Enter}
}
this script returns from 3 to 9 rows

I want 3 rows
as indicated after the "loop" (loop 3)

I have done many tests
I put "goto" after "Send, {Enter}" to go up the "loop3" but script becomes a loop
I tried with return, gosub, break, continue

thanks!
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: goto becomes loop

29 Feb 2016, 13:38

Try doing this?

Code: Select all

f3::
loop 3
{
random, m, 1, 3
gosub %m%
}
return

1:
random, k, 1, 2
Send, {a}%k%
Send, {Enter}

2:
random, k, 1, 2
Send, {b}%k%
Send, {Enter}
 
3:
random, k, 1, 2
Send, {c}%k%
Send, {Enter}
return
This would purposefully make it so if m is a value of 1, that it'll do all the label 1, 2, and 3 sections. I assume that's what you want because you said you want the script to return between 3 and 9 rows.

It is untested code so I apologize if it doesn't work.

(Edit: Original syntax of Send, {a}% k% was wrong from original post; corrected it here.)
Last edited by Exaskryz on 29 Feb 2016, 19:18, edited 1 time in total.
wizardzedd
Posts: 319
Joined: 23 Jan 2016, 23:03

Re: goto becomes loop

29 Feb 2016, 15:42

Unless the labels are needed for something else, I would recommend using if and else if . The trouble you are having now is that if it goes to 1 it will execute everything beneath it, including 2 and 3. Likewise 2 would also execute 3's code. 3 would be the only one to execute only it's own code.

Code: Select all

	F3 ::
	loop 3
	{
		random, m, 1, 3
		random, k, 1, 2
		if(m = 1) {
			Send, {a}% k%
			send, {Enter}
		} else if(m = 2) {
			Send, {b}% k%
			Send, {Enter}
		} else if(m = 3) {
			Send, {c}% k%
			Send, {Enter}
		}
	}
Balduz
Posts: 2
Joined: 29 Feb 2016, 11:42

Re: goto becomes loop

29 Feb 2016, 16:11

@Exaskryz
thanks for the reply
first of all I do not know why the script that I have posted has the "%k%" value out of the brace (})
I tried your suggestion but returns from 3 to 9 rows (like my script in the first post)
sorry for my english.
I have explained bad.
I want 3 rows. not more, not less
---
example:

press F3 =
cc
aaa
b

again press F3 =
bbb
c
cc

again press F3 =
etc..............
---
I want three rows (depending on the value after loop, example "loop 5" = 5 rows) formed each one from 1 to 3 letters.
random is the number of letters in the row. from 1 to 3
and random letter. from "a" to "c"

@wizardzedd
thank you!!! your script is perfect
I have to study better the commands if and else if
I started the day before yesterday with AHK

Code: Select all

F3::
	loop 3
	{
		random, m, 1, 3
		random, k, 1, 2
		if(m = 1) {
			Send, {a %k%}
			send, {Enter}
		} else if(m = 2) {
			Send, {b %k%}
			Send, {Enter}
		} else if(m = 3) {
			Send, {c %k%}
			Send, {Enter}
		}
	}
works well

I would have liked to use the labels :)
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: goto becomes loop

29 Feb 2016, 17:29

To get 3 letters in a row you would need to change to:

Code: Select all

random, k, 1, 3

1 liner send for fun example:

Code: Select all

F3::
	loop 3
	{
		random, m, 1, 3
		random, k, 1, 3
		Send, % "{" (m=1?"a":m=2?"b":"c") A_Space k "}{Enter}"
	}
return
some more fun:

Code: Select all

F3::
	loop 3
	{
		random, m, 97, 99
		random, k, 1, 3
		Send, % "{" chr(m) A_Space k "}{Enter}"
	}
return
Also note: if you are going to check for 3 items using if / if else the 3rd or last item should just be else because you don't need to check what it is since its not the 1st 2 items out of 3 you are checking.
wizardzedd
Posts: 319
Joined: 23 Jan 2016, 23:03

Re: goto becomes loop

29 Feb 2016, 17:49

Using labels, I do not think anyone should ever do this unless they absolutely need to (like having a timer somewhere else to run the label or something, but even then you probably should use a function unless you are in a really old version of AHK)...

Code: Select all

F3::
loop 3
{
	random, m, 1, 3
	random, k, 1, 2
	goto, %m%
	1:
		Send, {a}%k%
		send, {Enter}
		continue
	2:
		Send, {b}%k%
		Send, {Enter}
		continue
	3:
		Send, {c}%k%
		Send, {Enter}
}
@Xtra I don't think he wants 3 of the same letter in a row, just 3 rows. Also good point on the if / if else , since it is neither 1 or 2, we know it must be 3. I figured I'd leave it just for the sake of clarity. Usually I would do something like this:

Code: Select all

random , x, 1, 3
if(x=1)
	; do something
else if(x=2)
	; do something else
else ; x=3
	; do something really special
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: goto becomes loop

29 Feb 2016, 19:16

Balduz wrote:I would have liked to use the labels :)
Probably better options to doing this script have been pointed out. But for the sake of learning, I'll show you how you could keep your labels and do what you want.

To make it so it only uses 3 rows, and to use labels, return to the script I suggested. And use the return at the end of each label, such as

Code: Select all

1:
random, k, 1, 2
Send, {a %k%} ; or {a}%k% ?
Send, {Enter}
return
And same for 2: and 3:.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mstrauss2021 and 212 guests