Re: One Line If Statements
Posted: 04 Mar 2019, 13:27
Thats not possible in AHK v1 as it is a breaking change.
Let's help each other out
https://www.autohotkey.com/boards/
https://www.autohotkey.com/boards/viewtopic.php?f=13&t=62478
Code: Select all
if (1 == 1), stuff to do
if ((1 = 1) && 2 = 2), stuff to do
if (1 = 1) || (2 = 2), stuff to do
if ((((1 = 1) || 2 = 2) || 3 = 3) || 4 = 4), stuff to do
Code: Select all
if (condition), alsoPartOfCondition
So 'bunching up lines' is the sole objective? I cannot detect a real advantage (and I wouldn't change the parser to achieve it).jeeswg wrote: ↑04 Mar 2019, 09:14- @just me:
......Code: Select all
if cond LINSEP action ;advantage: a generally useful feature (for bunching up lines)
Code: Select all
;IF ELSE
vNum1 := vNum2 := vNum3 := 0
Loop, 10
{
if (A_Index = 1)
vNum1++, vNum2++, vNum3++
else if (A_Index = 3)
{
vNum1++, vNum2++, vNum3++
continue
}
else if (A_Index = 5)
{
vNum1++, vNum2++, vNum3++
break
}
else if (A_Index = 7)
vNum1++, vNum2++, vNum3++
}
MsgBox, % vNum1 " " vNum2 " " vNum3
;==================================================
;SWITCH
vNum1 := vNum2 := vNum3 := 0
Loop, 10
{
switch
{
case (A_Index = 1): vNum1++, vNum2++, vNum3++
case (A_Index = 3): vNum1++, vNum2++, vNum3++
continue
case (A_Index = 5): vNum1++, vNum2++, vNum3++
break
case (A_Index = 7): vNum1++, vNum2++, vNum3++
}
}
MsgBox, % vNum1 " " vNum2 " " vNum3
;==================================================
;SWITCH VAR
vNum1 := vNum2 := vNum3 := 0
Loop, 10
{
switch A_Index
{
case 1: vNum1++, vNum2++, vNum3++
case 3: vNum1++, vNum2++, vNum3++
continue
case 5: vNum1++, vNum2++, vNum3++
break
case 7: vNum1++, vNum2++, vNum3++
}
}
MsgBox, % vNum1 " " vNum2 " " vNum3
;==================================================
return
Unlike the switch statement found in some other languages, there is no implicit fall-through and Break is not used (except to break out of an enclosing loop).
Code: Select all
;before:
if (condition)
{
ismatch := 1
break
}
;after:
if (condition)
ismatch := 1
, break
Code: Select all
+Enter::
BraceTheCode()
{
ClipSaved := ClipboardAll
Clipboard := ""
Send ^c
ClipWait 0.2
Code := Clipboard
if !RegExMatch(Code, "\R$")
{
if !RegExMatch(Code, "^\R")
{
Clipboard := RegExReplace(Code, "`am)^(?<=`n)(?=.)", " ")
Send {{}{Enter}{}}{Up}{End}{Enter}^v
}
else
{
Clipboard := Code
Send {Space}{{}{Enter}{}}{Up}{End}^v
}
}
Sleep 200
Clipboard := ClipSaved
}
Code: Select all
if (condition)
break
;version 1, not possible
if (condition)
MsgBox(var)
, break
;version 2
if (condition)
{
MsgBox(var)
break
}
;version 3
if (condition)
MsgBox(var)
if (condition)
break
Do you even use AutoHotkey?So that means adding in braces/indentation etc.
Code: Select all
;current:
{
var := 1
break
}
;not possible:
var := 1, break
;not possible:
var := 1
, break
Code: Select all
;e.g. before:
; var := 1
;e.g. after:
;{
; var := 1
; break
;}
;to use: make the line you want to wrap, the current line,
;then trigger the hotkey
#IfWinActive ahk_class Notepad
^#b:: ;notepad - insert braces and 'break'
^#c:: ;notepad - insert braces and 'continue'
vCFS := InStr(A_ThisHotkey, "b") ? "break" : "continue"
ControlGet, hCtl, Hwnd,, Edit1, A
ControlGet, vLineNum, CurrentLine,,, % "ahk_id " hCtl
ControlGet, vText, Line, % vLineNum,, % "ahk_id " hCtl
vPos := RegExMatch(vText, "[^`t]")
if (vPos = 1)
vText := "`t" vText, vPos := 2
;vIndent := StrRept("`t", vPos-2)
vIndent := ""
Loop, % vPos-2
vIndent .= "`t"
SendInput, {Home}+{End}
vText := vIndent "{`n" vText "`n" vIndent "`t" vCFS "`n" vIndent "}`n"
;SendInput, % "{Text}" vText
vText := StrReplace(vText, "`n", "`r`n")
Control, EditPaste, % vText,, % "ahk_id " hCtl
return
#IfWinActive
It's surprised me, that you haven't wanted, from early on: ObjCount, StrRept (which I needed above), InputBox font size, OnHotkey for logging hotkey use, and other friends. Although, you did implement the vast majority of what I wanted, before I joined the forum. Cheers.Do you even use AutoHotkey?
Code: Select all
If (1 = 1) msgbox true
If (1 = 1), msgbox true
If (1 = 1) { msgbox true }
If (1 = 1) then msgbox true
Code: Select all
(DoMsg) && MsgBox(True)
(SkipMessage) || MsgBox(True)
I think that's more clear. Personally I'm thankful that ahk v2 has simplified so much the usage of commas, I wouldn't ever want go back to seeing stuff like If (1 = 1), msgbox true.SandyClams wrote: ↑01 Sep 2021, 13:04if you want a one-line if in v2 you can just use a short-circuit expression to do it.
Code: Select all
(DoMsg) && MsgBox(True) (SkipMessage) || MsgBox(True)