While AutoHotkey Does Not Include a Conditional Case Statement, You Can Build Your Own Using the Ternary Operator
Note, January 13, 2020: AutoHotkey now include a Switch command with more flexibility and the same speed as this pseudo-switch.
Many programming languages include Case statements which act in a manner to similar a series of If…Else If…Else statements in an abbreviated form. The simplicity of the structure provides the primary benefit of offering a series of options in semi-list form (taken from Microsoft Docs SQL example, “Using a SELECT statement with a simple CASE expression”):
USE AdventureWorks2012; GO SELECT ProductNumber, Category = CASE ProductLine WHEN 'R' THEN 'Road' WHEN 'M' THEN 'Mountain' WHEN 'T' THEN 'Touring' WHEN 'S' THEN 'Other sale items' ELSE 'Not for sale' END, Name FROM Production.Product ORDER BY ProductNumber; GO
As you can see in the above example, the center of the expression includes an easily altered list of possibilities. This can make changing, adding, deleting items a snap. Unfortunately, AutoHotkey does not include a Case command. However, you can simulate any Case statement with a series of If…Else If…Else conditionals:
If Var = 'R' {Value := 'Road'} Else If Var = 'M' {Value := 'Mountain'} Else If Var = 'T' {Value := 'Touring'} Else If Var = 'S' {Value := 'Other sale items'} Else {Value := 'Not for sale'}
To further reduce the syntax, use the Ternary operator:
Value := Var = 'R' ? 'Road' : Var = 'M' ? 'Mountain' : Var = 'T' ? 'Touring' : Var = 'S' ? 'Other sale items' :'Not for sale'
While this looks similar to a series of switches, it actually represents one continuous line of code using a cascading series of Ternary If-Else statements. It uses line-continuation techniques to split the line into its component pieces. The following statement:
var := [expression] ? [if yes] : [if no expression] ? [if yes] : [if no]
turns into:
var := [expression] ? [if yes] : [if no expression] ? [if yes] : [if no]
The colon tells AutoHotkey to read the line as if it belongs to the previous line. Without line-continuation techniques, the code would quickly devolve into an unwieldy statement in any text editor. We can continue to add conditionals to the list by using this abbreviated method.
In the DateStampConvert.ahk script, we use this technique to convert the name of a month into its corresponding numeric value.
The MonthConvert() Function
The alphanumeric forms of the date format contain text values for the names of the months. This works great for identifying the type of format (see the RegExs in “Using Regular Expressions to Convert Most Formatted Dates into DateTime Stamps“) but doesn’t work in the all-numeric DateTime Stamp format. We must convert any text into its numeric equivalent.
The MonthConvert() function in the DateStampConvert.ahk script uses a series of cascading Ternary operators to simulate Case statements or switches. While technically one line of code, the statement uses line-continuation techniques to break it into a logical item list for easy reading and editing:
MonthConvert(month) { Global NewMonth NewMonth := InStr(month, "jan") ? "01" : InStr(month, "feb") ? "02" : InStr(month, "mar") ? "03" : InStr(month, "apr") ? "04" : InStr(month, "may") ? "05" : InStr(month, "jun") ? "06" : InStr(month, "jul") ? "07" : InStr(month, "aug") ? "08" : InStr(month, "sep") ? "09" : InStr(month, "oct") ? "10" : InStr(month, "nov") ? "11" : InStr(month, "dec") ? "12" :"Not found!" }
As its parameter, the function uses the text name of any month of the year (regardless of word length or capitalization) which contains the common three-letter abbreviation (e.g. jan, feb, mar…). The function returns the numeric value for any valid month name.
Any time you need a list of options, consider this conditional technique.
This post was proofread by Grammarly
(Any other mistakes are all mine.)
(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)
[…] Previous Blogs, I Used Regular Expressions to ID Dates Formats in Documents and Simulated Case/Switch Statements to Convert Month Names to Numbers. Now, I Build the Standard DateTime Stamp, Check for Valid Dates, […]
LikeLike
[…] “AutoHotkey Toggles and the Ternary Operator (Beginning Hotkeys Part 18)” or “Use the Ternary Operator to Create Conditional Case Statements or Switches (AutoHotkey Tip)” for more information on the ternary operator.) The AddHotstring routine which calls the […]
LikeLike
[…] to convert English text month names into their numeric values within a single function (“Use the Ternary Operator to Create Conditional Case Statements or Switches“). The ternary operator shortcut acts as If-Else statements in abbreviated […]
LikeLike
If Else Var = ‘M’ {Value := ‘Mountain’}
If Else Var = ‘T’ {Value := ‘Touring’}
If Else Var = ‘S’ {Value := ‘Other sale items’}
on
https://jacks-autohotkey-blog.com/2018/11/02/use-the-ternary-operator-to-create-conditional-case-statements-or-switches-autohotkey-tip/
Should these be
Else If Var = ‘M’ {Value := ‘Mountain’}
Else If Var = ‘T’ {Value := ‘Touring’}
Else If Var = ‘S’ {Value := ‘Other sale items’}
LikeLike
Thanks! Corrected.
LikeLike