Page 1 of 1

New Switch/Case Command for Alternative Conditions

Posted: 15 Jan 2020, 09:39
by jackdunning
Rather than using a series of If-Else Statements (or the ternary operator), the new Switch command sets up Case statements for alternative results.

In the DateStampConvert.ahk script, the MonthConvert(month) function uses a series of ternary operators to match the text month abbreviation to the corresponding numeral:

Code: Select all

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!"
}
I rewrote the MonthConvert() function for the DateStampConvertSwitch.ahk script using the new AutoHotkey Switch command to replace the cascading ternary operators used in the original DateStampConvert.ahk script.

Code: Select all

MonthConvert(month)
{
	Switch SubStr(month,1,3)
	{	
		Case "jan","ene","jän","gen":Return "01" 
		Case "feb","fév","fev": Return "02"
		Case "mar","mär": Return "03"
		Case "apr","abr","avr": Return "04"
		Case "may","mai","mag": Return "05"
		Case "jun","jui","gui":
			If (SubStr(month,1,4) = "juil")
				 Return "07"
			Else
				 Return "06"
		Case "jul","lug": Return "07"
		Case "aug","ago","aoû","aou","ag": Return "08"
		Case "sep","set": Return "09"
		Case "oct","okt","ott": Return "10"
		Case "nov": Return "11"
		Case "dec","dic","dez","déc": Return "12"
  	}
}
I found that the new syntax made setting up a lookup table of values easier by eliminating the need to carefully track the multiple If-Else statements. I don’t know that it works any better or faster, but the framework makes understanding, implementation, and changing the conditions simpler for the user.

A few basic rules:
  1. A command statement can appear either on the same line as the Case statement (all one line) or start on the next line—including any number of command statements.
  2. Unlike multiple line If-Else statements, Case statements do not require curly brackets to enclose multiple command statement lines. (No need to keep track of curly brackets.)
  3. Up to 20 matching expression values (separated by commas) can appear after the Case command but before the closing colon. These values may come in the form of text, numbers, variables, or functions. The delimiting commas act in the same manner as a logical OR operator between each value (OR or ||) in an If-Else statement.
  4. The Switch-Case condition can either match an optional SwitchValue expressed in the Switch command line (Switch [SwitchValue]) or execute the first Case statement found true.
  5. Although not explicitly stated, the Switch command likely uses Short-circuit Boolean Evaluation. That means after finding a match and executing the enclosed Case commands, AutoHotkey exits the Switch without further evaluation.
  6. It seems that you can add GoTo Labels to the Case statements to allow jumps inside the Switch block. While I haven’t thought through how to use this type of Label jump, AutoHotkey does not support this type of movement between If and Else blocks.
Be sure to update AutoHotkey to the latest release (November 2019) for Switch/Case command support.

Plus, this DateStampConvertSwitch.ahk script, as shown in the Switch statement above, also supports converting dates formatted in Spanish, German, French, and Italian month names. See "Jack's AutoHotkey Blog" (January 13, 2020) for more information.

Re: New Switch/Case Command for Alternative Conditions

Posted: 15 Jan 2020, 12:46
by Delta Pythagorean
Case and Switch was added in 1.1.31.00, relatively new.
With that said, here's the basic documentation on the new items for those who want to know how to use it.

Re: New Switch/Case Command for Alternative Conditions

Posted: 15 Jan 2020, 12:50
by swagfag
nice writeup, although i wouldnt necessarily recommend using Switch for lookup tables

Re: New Switch/Case Command for Alternative Conditions

Posted: 15 Jan 2020, 13:53
by JoeWinograd
Delta Pythagorean wrote:
15 Jan 2020, 12:46
Case and Switch was added in 1.1.31.00, relatively new.
Yes, but there were a couple of serious bugs in it that were fixed in v1.1.31.01, as I mentioned in the 13-Oct-2019 update to my 2-Oct-2019 article:
AutoHotkey Switch-Case

It's working very well now in v1.1.32.00! Regards, Joe

Re: New Switch/Case Command for Alternative Conditions

Posted: 16 Jan 2020, 10:32
by Helgef
Hi.
Although not explicitly stated, the Switch command likely uses Short-circuit Boolean Evaluation. That means after finding a match and executing the enclosed Case commands, AutoHotkey exits the Switch without further evaluation.
It is stated in the docs,
Switch wrote: If present, SwitchValue is evaluated once and compared to each case value until a match is found, and then that case is executed. Otherwise, the first case which evaluates to true (non-zero and non-empty) is executed.
It seems that you can add GoTo Labels to the Case statements to allow jumps inside the Switch block. While I haven’t thought through how to use this type of Label jump, AutoHotkey does not support this type of movement between If and Else blocks.
You can,
Switch wrote: As all cases are enclosed in the same block, a label defined in one case can be the target of Goto from another case.
You can use it to, for example, mimic fall-through, eg

Code: Select all

switch "a" {
	case "a":
		; ...
		goto case_b
	case "b":
		case_b:
		; ...
}
Thanks for sharing, cheers.