Tutorial of some One Line Logic in AutoHotkey

Helpful script writing tricks and HowTo's
User avatar
Joe Glines
Posts: 420
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

Tutorial of some One Line Logic in AutoHotkey

17 Jul 2016, 08:33

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

[youtube]https://www.youtube.com/watch?v=LtVnBANvXfU[/youtube]

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus







One line logic in AHK

[youtube]https://www.youtube.com/watch?v=_WkChKF5J2o[/youtube]

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

TAC109
Posts: 178
Joined: 02 Oct 2013, 19:41

Re: Tutorial of some One Line Logic in AutoHotkey

17 Jul 2016, 17:42

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

User avatar
Joe Glines
Posts: 420
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

Re: Tutorial of some One Line Logic in AutoHotkey

17 Jul 2016, 18:54

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.
User avatar
Joe Glines
Posts: 420
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

Re: Tutorial of some One Line Logic in AutoHotkey

20 Jul 2016, 06:36

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] [Download] GeSHi © Codebox Plus

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: 191
Joined: 24 Jun 2015, 23:38

Re: Tutorial of some One Line Logic in AutoHotkey

20 Jul 2016, 14:48

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.
User avatar
Joe Glines
Posts: 420
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

Re: Tutorial of some One Line Logic in AutoHotkey

20 Jul 2016, 15:41

@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)
:o:tern.::

Code: [Select all] [Download] GeSHi © Codebox Plus

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

Re: Tutorial of some One Line Logic in AutoHotkey

01 Mar 2017, 07:11

This tutorial thread is about about ternary operators for performing logic on a single line, so it shouldn't be derailed on a different subject. Please create a new thread in "Ask For Help" for your question.

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

Re: Tutorial of some One Line Logic in AutoHotkey

01 Mar 2017, 08:22

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
TAMIL NADU
sv270190
Posts: 43
Joined: 06 Feb 2014, 11:48
Facebook: svsammaponnu@gmail.com

Re: Tutorial of some One Line Logic in AutoHotkey

01 Mar 2017, 09:20

Got it thanks
S.V. SRINIVASAN
SRIVILLIPUTTUR
TAMIL NADU

Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 4 guests