Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell switch statement comparison value 1d erractic, why?

$UserChoice = Read-Host "Enter # of tool you want to run"
switch -exact ($UserChoice) {
    1 {Write-Host 'You selected 1'}
    1a {Write-Host 'You selected 1a'}
    1b {Write-Host 'You selected 1b'}
    1c {Write-Host 'You selected 1c'}
    1d {Write-Host 'You selected 1d'}
}

Preface: I know the fix is to put the comparison values in quotes, ex: '1d'. I want to know WHY PowerShell is acting the way it is for learning sake.

  1. If I enter 1b at prompt, it returns 1b whereas if I enter 1d it returns nothing, why?
  2. If I enter 1b at prompt, it returns 1b whereas if I enter 1 it returns 1 and 1d, why?
  3. It seems PowerShell is interpreting d in a switch statement comparison value different for some reason, why?

I noticed VISUALLY the comparison values are different colors (1 & 1d are darker) so I guess PowerShell ISE syntax highlighting is telling us they're being treated differently. 4. What do the colors mean (maroon/dark-RedOrPurple VS purple? enter image description here

like image 976
gregg Avatar asked Sep 15 '25 17:09

gregg


1 Answers

Before explaining why the 1d label is "special", I should note that the -exact mode (which is the default mode of comparison for a switch statement) is probably a bit misleading.

It simply means "use the -eq operator to compare input values to case labels".

The reason 1d behaves differently is that PowerShell doesn't recognize the expression 1d as a string. Instead, it interprets d is a numerical suffix signifying the [decimal] type, and the case label value is thus the same as if you'd written 1.0 or $([decimal]1).

The result is that comparison to the input string "1" comes out the same for both - "1" -eq 1 and "1" -eq 1d are both true, thanks to PowerShell's overloaded operators.

If you ever expand your options further, you'll encounter the same problem with 1l (l = [long]), and, if using PowerShell 7, eventually 1n, 1s, 1u, and 1y.

Quote the switch labels to avoid PowerShell parsing them as a numerical expressions:

$UserChoice = Read-Host "Enter # of tool you want to run"
switch -exact ($UserChoice) {
    '1'  {Write-Host 'You selected 1'}
    '1a' {Write-Host 'You selected 1a'}
    '1b' {Write-Host 'You selected 1b'}
    '1c' {Write-Host 'You selected 1c'}
    '1d' {Write-Host 'You selected 1d'}
}

See the about_Numeric_Literals help topic for a more comprehensive overview of suffixes interpreted as numerical modfiers by PowerShell.

like image 88
Mathias R. Jessen Avatar answered Sep 18 '25 10:09

Mathias R. Jessen