Use the Ternary Operator to Create Conditional Case Statements or Switches (AutoHotkey Tip)

While AutoHotkey Does Not Include a Conditional Case Statement, You Can Build Your Own Using the Ternary Operator

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 formLibrary Benefits (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'}  
  If Else Var = 'M' {Value := 'Mountain'}  
  If Else Var = 'T' {Value := 'Touring'}  
  If Else 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.

jack

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.)

Advertisements

One thought on “Use the Ternary Operator to Create Conditional Case Statements or Switches (AutoHotkey Tip)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s