Tutorial of some One Line Logic in AutoHotkey

Helpful script writing tricks and HowTo's
User avatar
Joe Glines
Posts: 772
Joined: 30 Sep 2013, 20:49
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

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, myVar,6,EnvAdd,myVar, 2 ;If myVar=6 add 2 to myVar
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
}
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
TAC109
Posts: 1154
Joined: 02 Oct 2013, 19:41
Location: New Zealand

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 scripts:-
XRef - Produces Cross Reference lists for scripts
ReClip - A Text Reformatting and Clip Management utility
ScriptGuard - Protects Compiled Scripts from Decompilation
I also maintain Ahk2Exe
User avatar
Joe Glines
Posts: 772
Joined: 30 Sep 2013, 20:49
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.
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
User avatar
Joe Glines
Posts: 772
Joined: 30 Sep 2013, 20:49
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

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
}
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
JJohnston2
Posts: 204
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: 772
Joined: 30 Sep 2013, 20:49
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

var =
( Join`r
data:= (Var="1")?("one")
      :(Var="2")?("two")
      :(Var="3")?("three")
      :("Else") 
MsgBox `% data
)
Send, %var%
return
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
User avatar
boiler
Posts: 17706
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: 45
Joined: 06 Feb 2014, 11:48

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
User avatar
Joe Glines
Posts: 772
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: Tutorial of some One Line Logic in AutoHotkey

01 Mar 2017, 09:05

Ternary is only meant for evaluating expressions. see this post https://autohotkey.com/board/topic/6898 ... and-in-it/
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
sv270190
Posts: 45
Joined: 06 Feb 2014, 11:48

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 (v1)”

Who is online

Users browsing this forum: No registered users and 37 guests