How to optimize this code Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Pettola
Posts: 6
Joined: 13 Jun 2017, 08:40

How to optimize this code

25 Feb 2018, 07:13

Hi, I wrote this code few days ago, but I was not able to write it in a short way.
As you can see there are 9 code blocks which are mostly the same, the only difference is the sending of F2, F3, etc.
So I wonder if there is a way to shrink the 9 blocks in a single block, maybe using a for cycle where for every iteration the last Send command get different F2, F3, etc. key.

the full code

Code: Select all

Alt::
InputBox, UserInput, Quanti ?, , , 150, 100
i = %UserInput%

Sleep 499
SendInput %i%
Sleep 50
Send {Return}
Sleep 999
Send {F2}
Sleep 1399

Click
Sleep 499
SendInput %i%
Sleep 50
Send {Return}
Sleep 999
Send {F3}
Sleep 1399

Click
Sleep 499
SendInput %i%
Sleep 50
Send {Return}
Sleep 999
Send {F4}
Sleep 1399

Click
Sleep 499
SendInput %i%
Sleep 50
Send {Return}
Sleep 999
Send {F5}
Sleep 1399

Click
Sleep 499
SendInput %i%
Sleep 50
Send {Return}
Sleep 999
Send {F6}
Sleep 1399

Click
Sleep 499
SendInput %i%
Sleep 50
Send {Return}
Sleep 999
Send {F7}
Sleep 1399

Click
Sleep 499
SendInput %i%
Sleep 50
Send {Return}
Sleep 999
Send {F8}
Sleep 1399

Click
Sleep 499
SendInput %i%
Sleep 50
Send {Return}
Sleep 999
Send {F9}
Sleep 1399

Click
Sleep 499
SendInput %i%
Sleep 50
Send {Return}
Return
idea for the solution

Code: Select all

for j from 1 to 9
{
Click
Sleep 499
SendInput %i%
Sleep 50
Send {Return}
Sleep 999
Send **component j of an array containing F2, F3, ..., F9**
Sleep 1399
}
is this possible?
Last edited by Pettola on 26 Feb 2018, 04:17, edited 1 time in total.
gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: How to optimize this code

25 Feb 2018, 13:18

Sure, these are very basic programming techniques that you can use in (almost) any programming language, you just have to use the right syntax. Look into loops, the built-in variable A_index - and probably if. If you are just counting up like in F1, F2... F8, you don't even need an array, just A_index - although you could use an array here, too.
User avatar
Renz
Posts: 20
Joined: 05 Sep 2015, 21:56
Location: Dortmund (NRW|GER)
Contact:

Re: How to optimize this code  Topic is solved

25 Feb 2018, 16:47

Maybe this can help you? Im not a pro :) its untested but i think, in the theory, it should work...
I try allways too use Expressions for my Codes...

Code: Select all

Alt::
InputBox, UserInput, Quanti ?, , , 150, 100
MAX_STEPS := 8
While(A_Index <= MAX_STEPS)
{
	if(A_Index == MAX_STEPS)
	{
		Click
		Sleep 499
		SendInput % UserInput
		Sleep 50
		Send % "{Return}"
		break
	}
	else
	{
		Click
		Sleep 499
		SendInput % UserInput
		Sleep 50
		Send % "{Return}"
		Sleep 999
		Send % "{" (A_Index == 1 ? "F2" : "F" (A_Index+1)) "}"
		Sleep 1399
	}
}
Return
Pettola
Posts: 6
Joined: 13 Jun 2017, 08:40

Re: How to optimize this code

26 Feb 2018, 05:01

gregster wrote:Sure, these are very basic programming techniques that you can use in (almost) any programming language, you just have to use the right syntax. Look into loops, the built-in variable A_index - and probably if. If you are just counting up like in F1, F2... F8, you don't even need an array, just A_index - although you could use an array here, too.
Thank you very much for the hints!
Renz wrote:Maybe this can help you? Im not a pro :) its untested but i think, in the theory, it should work...
I try allways too use Expressions for my Codes...
[/code]
Wow it works like a charm thank you very much!
I just had to put MAX_STEPS equal to 9 otherwise F9 key would not be sent.
Anyway, could you explain a bit the line

Code: Select all

Send % "{" (A_Index == 1 ? "F2" : "F" (A_Index+1)) "}"
?
It seems like you are "building" the object to send by composing it character by character: "{" + F2 (if A_Index == 1) or FA_Index+1 (otherwise) + "}"
User avatar
Renz
Posts: 20
Joined: 05 Sep 2015, 21:56
Location: Dortmund (NRW|GER)
Contact:

Re: How to optimize this code

26 Feb 2018, 13:37

A_Index is equal the times the loop is running...
Send % "{" (A_Index == 1 ? "F2" : "F" (A_Index+1)) "}"
in this part the ternary operator will check if loop runs the first time,
if the loop runs the first time, ternary operator will set "F2" cause the Expression A_Index == 1 is true!
and if the loop is running the 2,3,4,5 etc time the ternary operator will set F+LoopCount+1 means F2,F3,F4 etc
the +1 is required cause u cant change the Value of A_Index, so u need to add 1 via a + sign...
i hope this will help you, and you understand what i mean :)
its a pretty powerful operator... i cant much say about whats have a better performance, but i think the if() is faster in a ScriptRun...

ternary operator example in the spoiler down
Spoiler
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: How to optimize this code

26 Feb 2018, 14:27

Pettola wrote:idea for the solution

Code: Select all

for j from 1 to 9
{
Click
Sleep 499
SendInput %i%
Sleep 50
Send {Return}
Sleep 999
Send **component j of an array containing F2, F3, ..., F9**
Sleep 1399
}
is this possible?
Implimentation of "idea" code:

Code: Select all

for Index, Key in ["{F2}", "{F3}", "{F4}", "{F5}", "{F6}", "{F7}", "{F8}", "{F9}"]
{
	Click
	Sleep 499
	SendInput %i%
	Sleep 50
	Send {Return}
	Sleep 999
	Send % Key
	Sleep 1399
}
This demonstrates how to use a for-loop to step through the elements of an array object.

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
Pettola
Posts: 6
Joined: 13 Jun 2017, 08:40

Re: How to optimize this code

27 Feb 2018, 04:59

Renz wrote:A_Index is equal the times the loop is running...
Send % "{" (A_Index == 1 ? "F2" : "F" (A_Index+1)) "}"
in this part the ternary operator will check if...
thanks for the detailed explanation!
FanaticGuru wrote:
Pettola wrote: Implimentation of "idea" code:
...
This demonstrates how to use a for-loop to step through the elements of an array object.

FG
Thank you very much for the lesson!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, DiegoSouto, mikeyww, Rohwedder, Sniperman and 382 guests