Skip to content

Instantly share code, notes, and snippets.

@joedf
Last active August 29, 2015 13:55
Show Gist options
  • Save joedf/8700813 to your computer and use it in GitHub Desktop.
Save joedf/8700813 to your computer and use it in GitHub Desktop.
a lazy 'switch' implementation/demonstration in AutoHotkey ???
; Switch-Ifs.ahk
;-----------------------------------------------------------------------------
; a 'switch' implementation/demonstration ??? First created @ 20:06 2014/01/29
; by joedf - Revision 2 - 17:49 2014/10/26
; to be combined with this ??? http://www.autohotkey.com/board/topic/37397-onelinecommands-execute-ahk-code-dynamically/
; Please note... that this is a very lazy implementation... :P
; MsgBox are there because of debugging.. left there for convenience?
;///////////////////////////////////////////////////////////
;
; Advantages/Disadvantages (Pros & Cons)
; --------------------------------------
; Switch() & Ifs() :
; - Simply returns strings for the moment due to lack of dynamic code executer
; - No Need to worry about "Duplicate function/label definitions"
; - Can be used multiple times
; - Needs to parse the "Cases" string to an Object
; SwitchObj() :
; - Runs Labels & Functions depending on their existence
; - Could run Dynamic Code, if function existent under the name of "DynamicExecute()"
; - Partial : No Need to worry about "Duplicate function/label definitions"
; - Can be used multiple times
; - Needs the "Cases" to be defined as an Object
; SwitchX() :
; - Pratical Example of a Pseudo-Switch Statement
; - Need to worry about "Duplicate function/label definitions"
; - Needs to be redefined many times
; - Can only be used as a "specific" switch statement
; TIP! : See "Tests" for example usage
; --------------------------------------
;
;///////////////////////////////////////////////////////////
; -------- [ Tests ] --------
; Test 1
x:="String with spaces" ; or 3, etc...
action:=switch(x,"Case 1: Msgbox AA %A_ThisCase%"
,"Case 2,3,4,5: Msgbox BB %A_ThisCase%"
,"Case String with spaces: Msgbox case name (string) : ""%A_ThisCase%"""
,"Case 6: Msgbox CC %A_ThisCase%"
,"Default: Msgbox ZZ %A_ThisCase%")
MsgBox Because X is equal to '%x%', the switch would execute the following action:`n`t%action%
; Test 2
y:=59
rvalue:=Ifs(y,"1,7,4:I am the return value if InputVar is 1, 7 or 4"
,"2,3,5:return alue for 2,3 and 5"
,"Default:I am the default return value")
MsgBox Because Y is equal to '%y%', Ifs() would Return the following:`n`t%rvalue%
; Test 3
SwitchVar := "Z"
SwitchObj({"Case A" :"TestA"
,"Case 8" :"Test8"
,"Case Z" :"TestA"
,"Case 2" :"MsgBox Hello World!"
,"Default":"TestDefault"}
,SwitchVar)
TestA() {
MsgBox SwitchObj() Case
}
TestDefault() {
MsgBox SwitchObj() Default
}
; Test 4
;////// example pseudo-Switch Statement
; from: http://www.autohotkey.com/board/topic/18276-switchcase-statement-for-autohotkey/#entry120077
SwitchX(11)
SwitchX(Case) {
GoTo % IsLabel("CaseX-" Case) ? "CaseX-" Case : "CaseX-Default"
CaseX-A:
MsgBox Case-A
Return
CaseX-12:
MsgBox Case-12
Return
CaseX-Default:
MsgBox Default
Return
}
;-----------------------------------------------------------------------------------------------
; -------- [ Functions ] --------
; with a dynamic code executor...?
switch(InputVar,cases*) {
sObj := Object()
sObj["Cases"] := ""
for each, case in cases
{
if (RegExMatch(case,"i)Case\s+[^:]+:",subpat)) { ;Case names ->> DIGITS_ONLY: "i)Case\s+[\d,]+:"
caseNums:=RegExReplace(RegExReplace(subpat,"i)case\s+"),":")
caseAction:=RegExReplace(case,subpat . "\s*")
;MsgBox [%caseNums%]=[%caseAction%]
loop, parse, caseNums, `,
sObj[A_LoopField] := RegExReplace(caseAction,"i)%A_ThisCase%",A_LoopField) ; or without %A_ThisCase% --> sObj[A_LoopField] := caseAction
sObj["Cases"] := sObj["Cases"] . "," . caseNums
}
if (RegExMatch(case,"i)Default\s*:",subpat)) {
caseAction:=RegExReplace(case,subpat . "\s*")
;MsgBox [Default]=[%caseAction%]
sObj["Default"] := RegExReplace(caseAction,"i)%A_ThisCase%","Default") ; or without %A_ThisCase% --> sObj[A_LoopField] := caseAction
}
}
sObj["Cases"] := SubStr(sObj["Cases"],2) ; 'switch' object now complete ;)
listcases:=sObj["Cases"]
;MsgBox [%InputVar%]=[%listcases%]
if InputVar in %listcases%
return sObj[InputVar] ; in the future --> DynAHKExec(code) ... --> return DynAHKExec(sObj[InputVar])
else
return sObj["Default"] ; in the future --> DynAHKExec(code) ... --> return DynAHKExec(sObj["Default"])
}
; without really having a dynamic code executor... it could used a quick multi 'if' as in 'ifs'?
Ifs(InputVar,cases*) {
sObj := Object()
sObj["Cases"] := ""
for each, case in cases
{
if (RegExMatch(case,"i)\s*[^:]+\s*+:",subpat)) {
caseNums:=RegExReplace(RegExReplace(subpat,"i)case\s+"),":")
caseAction:=RegExReplace(case,subpat . "\s*")
loop, parse, caseNums, `,
sObj[A_LoopField] := RegExReplace(caseAction,"i)%A_ThisCase%",A_LoopField)
sObj["Cases"] := sObj["Cases"] . "," . caseNums
}
if (RegExMatch(case,"i)Default\s*:",subpat)) {
caseAction:=RegExReplace(case,subpat . "\s*")
sObj["Default"] := RegExReplace(caseAction,"i)%A_ThisCase%","Default")
}
}
sObj["Cases"] := SubStr(sObj["Cases"],2)
listcases:=sObj["Cases"]
if InputVar in %listcases%
return sObj[InputVar]
else
return sObj["Default"]
}
;With "Object" Syntax, Will use a Dynamic Code executer named "DynamicExecute()" only ...
;if Existent and Accompaning var in the object is nor a Label or Function and is a valid Expression.
;if it is nor a Label or Function and "DynamicExecute()" is non existant, then the string itself is returned.
SwitchObj(e,case:="") {
i := e["Case " case]
d := e["Default"]
r := (i)?i:d
if IsLabel(r)
gosub, % r
if IsFunc(r)
return %r%()
;else IsExpr(r)
else if (f := Func("DynamicExecute"))
return % f.(r) ; return DynamicExecute(r)
return r
}
;-----------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment