Tutorial of some One Line Logic in AutoHotkey

Helpful script writing tricks and HowTo's
Joe Glines
Posts: 563
Joined: 30 Sep 2013, 20:49
GitHub: joetazz
Location: Dallas
Contact:

Tutorial of some One Line Logic in AutoHotkey

I played with the Ternary operator as well as using logic in one line. Here are two videos as well as the examples I review

Demystifying the Ternary operator

Code: Select all

``````Browser_Forward::Reload
Browser_Back::
var:="2"
;~ Ternary operator
;~    (condition)?(True)    : (False)
data:= (var="3")?("is one"):("Not One")
MsgBox % data

data:= (Var="1")?("one")
:(Var="2")?("two")
:(Var="3")?("three")
:("Else")
MsgBox % data

data := (Var="1")?("one"):(Var="2")?("two"):(Var="3")?("three"):("Else")
MsgBox % data``````

One line logic in AHK

Code: Select all

``````A_enter:="`r`n"
Browser_Back::
my:="hello"
var=
(
one
two
three

five
six
seven
)

Loop, parse, Var, `n, `r ;loop over Var line by line
{
myVar:=A_Index ;increment myVar

IfEqual, A_Index,1,continue ;skip processing header/1st row
IfEqual, A_LoopField,,continue ;Skip loop if A_Loopfiled is blank
IfEqual, A_Index,3,SetEnv,Data,New Value ;If A_Index is 3 set data:="New Value"
IfEqual, A_Index,5, Sleep, 2000 ;Sleep for 2 seconds on 3
IfEqual, A_LoopField,three,break ;Note- need to use percents if variable
IfEqual, A_index, 4, ExitApp ;Exit app if get to 10
IfLess, A_Index,6,FileAppend,%A_Loopfield%`r`n,test.txt,utf-8  ;If A_Index <6, write A_loopfield to test.txt
IfEqual, A_Index,4,ListVars

MsgBox % "index: " A_Index A_Enter "LoopField: " A_LoopField A_Enter "Data: " Data A_Enter "myVar: " myVar
}
``````
TAC109
Posts: 231
Joined: 02 Oct 2013, 19:41
Location: New Zealand

Re: Tutorial of some One Line Logic in AutoHotkey

Thanks for the tutorials.

I also prefer to write one-liners like you show in these videos. This hard to do because AHK is very 'line-centric', i.e. In general only one statement per line. I would like the language to also allow for, while, and loop statements to permit their controlled commands to be on the same line, but previous forum discussions on this have not swayed the principal coder.

BTW I noticed in your first video that you enclosed your numeric literals in quotes, rather than just writing them direct. Is this just a personal preference or was there some particular reason behind this?
My programs:-
ReClip - a Text Reformatting and Clip Management utility
XRef - Produces Cross Reference lists for scripts
Joe Glines
Posts: 563
Joined: 30 Sep 2013, 20:49
GitHub: joetazz
Location: Dallas
Contact:

Re: Tutorial of some One Line Logic in AutoHotkey

I agree, 1-line is very handy.

I know the While loop can typically use fewer lines that the other loops but agree it isn't as ideal I'd like.

No, the quotes around the number was just because I'd adopted the syntax from when I had words in them.
Joe Glines
Posts: 563
Joined: 30 Sep 2013, 20:49
GitHub: joetazz
Location: Dallas
Contact:

Re: Tutorial of some One Line Logic in AutoHotkey

Here are a few more examples that I made in my SciTE Output function.

I didn't realize I could SendMessage nor did I realize I could use IfMsgBox

Code: Select all

``````SciTE_Output(Text,Clear=1,LineBreak=1,Exit=0){
SciObj := ComObjActive("SciTE4AHK.Application") ;get pointer to active SciTE window
IfEqual,Clear,1,SendMessage,SciObj.Message(0x111,420) ;If clear=1 Clear output window
IfEqual, LineBreak,1,SetEnv,Text,`r`n%text% ;If LineBreak=1 prepend text with `r`n
SciObj.Output(Text) ;send text to SciTE output pane
IfEqual, Exit,1,MsgBox, 36, Exit App?, Exit Application? ;If Exit=1 ask if want to exit application
IfMsgBox,Yes, ExitApp ;If Msgbox=yes then Exit the appliciation
}``````
JJohnston2
Posts: 193
Joined: 24 Jun 2015, 23:38

Re: Tutorial of some One Line Logic in AutoHotkey

I like the ternary demo code showing it in a multi-line view for multiple selections... I hadn't thought about it doing it that way before (or at least initially before collapsing it into a single line)--it makes a multi-case ternary statement very clear.
Joe Glines
Posts: 563
Joined: 30 Sep 2013, 20:49
GitHub: joetazz
Location: Dallas
Contact:

Re: Tutorial of some One Line Logic in AutoHotkey

@JJohnston2 - Agreed! In the past when I had more than one decision-point I wouldn't use Ternary for that exact reason! Now it is a breeze! (Especially since I set up the following hotstring to give me a quick template to follow)
:tern.::

Code: Select all

``````var =
( Join`r
data:= (Var="1")?("one")
:(Var="2")?("two")
:(Var="3")?("three")
:("Else")
MsgBox `% data
)
Send, %var%
return``````
boiler
Posts: 2372
Joined: 21 Dec 2014, 02:44

Re: Tutorial of some One Line Logic in AutoHotkey

I suggest the mods delete these last two posts from this thread.
sv270190
Posts: 43
Joined: 06 Feb 2014, 11:48

Re: Tutorial of some One Line Logic in AutoHotkey

Sorry sir my question is how to run command /function A if va=1. If var=2 run command B if var=3 run, command C like that
S.V. SRINIVASAN
SRIVILLIPUTTUR
Joe Glines
Posts: 563
Joined: 30 Sep 2013, 20:49
GitHub: joetazz
Location: Dallas
Contact:

Re: Tutorial of some One Line Logic in AutoHotkey

Ternary is only meant for evaluating expressions. see this post https://autohotkey.com/board/topic/6898 ... and-in-it/
sv270190
Posts: 43
Joined: 06 Feb 2014, 11:48