Extended parameters

Put simple Tips and Tricks that are not entire Tutorials in this forum
User avatar
rommmcek
Posts: 1473
Joined: 15 Aug 2014, 15:18

Extended parameters

19 Oct 2020, 17:44

Extended parameters (unofficial tutorial)

Preface for AutoHotkey v1.1

In Autohotkey scripts virtually any parameter wich supports expressions can be extended. I didn't found this in docs. First I applied it when using Ternary operator e.g.: a? (b:=1, c:=0): (b:=0, c:=1), then I started to use it spontaneously alongside other parameters and suddenly I noticed it works everywhere.
See additional example (I wasn't aware of) by Rohwedder too!
Note: First parameter is considered as a native one!
As for every fee code and especially for this one: Test it and then use it at your own risk!

If (expression)

Code: Select all

a:= 0, b:= 0, c:= 4, d:= 5
loop, 2
    if (a<b, A_Index=1? b:=1: b:=-1, c++, d++)
        MsgBox % "Yes for the second turn even if the b is " b "`nwhile c: " c " and d: " d " are alway incremented"
    else MsgBox % "No even if b is " b "`nwhile c: " c " and d: " d " are alway incremented"

loop 
    if (A_Index>5? v:="Breaking the Loop occurred at iteration": "", l:=A_Index)
        Break
MsgBox % v " : " l

ExitApp
Loop (normal), (parse a string)

Code: Select all

a:= 3
loop, % (a, b:=4, c:=5)
    if A_Index = 1
        MsgBox  % "a = " a
    else if A_Index = 2
        MsgBox  % "b = " b
    else if A_Index = 3
        MsgBox  % "c = " c
SoundBeep   

loop, parse, % ("abc", a:=1, b:=2, c:=3) ; Note "parse" won't except extended parametrs
    if A_Index = 1
        MsgBox  % A_LoopField " = " a
    else if A_Index = 2
        MsgBox  % A_LoopField " = " b
    else if A_Index = 3
        MsgBox  % A_LoopField " = " c
SoundBeep        

loop, parse, % ("a`nb`nc", a:=1, b:=2, c:=3), % ("`n", d:=4)
    if A_Index = 1
        MsgBox  % A_LoopField " = " a
    else if A_Index = 2
        MsgBox  % A_LoopField " = " b
    else if A_Index = 3
        MsgBox  % A_LoopField " = " c
    MsgBox % "d = " d

ExitApp
While-loop

Code: Select all

a:= 3
while (a>0, b:=1, c:=A_Index, a-=b)   ;Note, that the third MsgBox appears despite a is beeing set to 0
    MsgBox % "a: " a "`nb: " b "`nc: " c
ExitApp
For-loop

Code: Select all

r:= [7, 8, 9]
for i, j in (r, b:=0, b+=1) ; parametr(s) are parsed only once
    MsgBox % "b is always: " b "`n" "j: " j
Random

Code: Select all

Random, Rn1, 1, (9, a:=11, b:=22) ; variable name of additional paramter may not be the same as the variable
Random, Rn2, (1, c:=33, d:=44), 9 ; to store random value, or it will be overwritten by Random command
Random, Rn3, (1, e:=55, f:=66), (9, g:=77, h:=88)
random, Rn4, (i:=a, i+=988), (j:=b, j+= 978)  ; note that chanage of the value effects the outcome immediately
MsgBox % Rn1 ";  " a ", " b "`n" Rn2 ";  " c ", " d "`n" Rn3 ";  " e ", " f ", " g ", " h "`nHowever: " Rn4 ";  " i ", " j 

ExitApp
SetTimer

Code: Select all

SetTimer, T, % (1000, a:=0, b:=10)
Return
Esc::ExitApp

T() {
    Global
    ToolTip % ++a ", " ++b
    Sleep 500
    ToolTip
}
Function parameters

Code: Select all

Global c:= 0
a:= 0, b:= 6
loop, 3
    MsgBox % f((a, c+=b), (b, b-=++a))
SoundBeep
ExitApp

f(a, b) {
    Return "a: " a "`nb: " b "`nc: " c
}
Everywhere...

Code: Select all

Gui, Add, % ("Edit", a:=9), % ("w200 h150 vEdt", whEdt:="w300 h250")
                          , % ("You can extend any parameter wich supports expressions!", SwitchEdt:=1)
Gui, Show, % ("", xyGui:= " x350 y350 ", whGui:=" w320 h265 ")

Sleep, % (1000, SwitchGui:=1)

if SwitchGui && SwitchEdt {
    GuiControl, Move, Edt, % whEdt
    Gui, Show, % xyGui whGui
}
MsgBox % "Varaible a is: " a
ExitApp
Return

GuiClose:
GuiEscape:
    ExitApp
Exemples for AutoHotkey v2.0
Last edited by rommmcek on 09 Dec 2020, 02:57, edited 2 times in total.
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: Extended parameters

09 Nov 2020, 15:02

rommmcek wrote:
19 Oct 2020, 17:44
While Loop...
Note, that the third MsgBox appears despite a is beeing set to 0
it should be ... ;)

Code: Select all

a:= 3
while (a>0 && (b:=1) && (c:=A_Index) && a-=b)   ;while (true and (true) and (true) and true)
    MsgBox % "a: " a "`nb: " b "`nc: " c
ExitApp
When you use commas only the first expression is evaluated (a>0) which is true three times.
Note: any assignment must be enclosed in parenthesis to be evaluated. (b:=1) && (c:=A_Index)


to have more fun I recommend also creating a msgbox() and a beep() function that returns "TRUE" for debugging.

Code: Select all

a:= 3
while (a>0 && msgbox(a) && (b:=1) && (c:=A_Index) && a-=b && beep())   ;while (true and true and (true) and (true) and true and true)
   continue
   
   msgbox(mymessage:="") {
	msgbox, % mymessage
	return true
}

beep(myFrequency:="", myDuration:="") {
	SoundBeep , myFrequency, myDuration
	return true
}



a return() function could also be very useful for one-liners
https://www.autohotkey.com/boards/viewtopic.php?p=319126#p319126

Code: Select all

return() {
Exit
}
Thank you for this great tutorial :thumbup:
User avatar
rommmcek
Posts: 1473
Joined: 15 Aug 2014, 15:18

Re: Extended parameters

12 Nov 2020, 19:41

Yeah, this is all about sparing one line and your way is possible workaround for the condition quirk (not needed in v2.0). Just came to know it is already around for ca. 14 years, can you believe it?

P.s.: I like to use ternaries and functions for one liner too and I find function returning true just for the sake of condition very astute!
Rohwedder
Posts: 7623
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Extended parameters

08 Dec 2020, 07:04

Hallo,
in my opinion you should add #IF expression, expression to your very interesting tutorial.

Code: Select all

LWin::vkFF
RWin::vkFF
#IF GetKeyState("vkFF"), vkFF:=SubStr(A_PriorHotkey,2)
q::MsgBox, you typed %vkFF% + Q
^+q::MsgBox, you typed %vkFF% + Ctrl + Shift + Q
#IF
User avatar
rommmcek
Posts: 1473
Joined: 15 Aug 2014, 15:18

Re: Extended parameters

09 Dec 2020, 02:53

Hi!
Thanks for your contribution. Actually I noticed your very sophisticated snippet in Ask For Help Forum and my jaw dropped twice! First for very effective Win blocking code, then for "extended parameter" (… oh, that works too!). At least I knew right away what the code does!
P.s.: I added a link to your post in OP.

Return to “Tips and Tricks (v1)”

Who is online

Users browsing this forum: No registered users and 29 guests