英文:
Searching for PowerShell syntax for multiple enum values in the case block of a switch statement?
问题
这是您要翻译的内容:
enum Fruit {
Apple
Banana
Orange
}
$myFruit = [Fruit]::$UsrString
switch ($myFruit) {
Apple, Orange {
Write-Host "这是苹果或橙子"
break
}
Banana {
Write-Host "这是香蕉"
break
}
default {
Write-Host "未知水果: $myFruit"
break
}
}
您可以将这段 PowerShell 代码用于枚举类型的多项情况。
英文:
An example of what I want to do:
enum Fruit {
Apple
Banana
Orange
}
$myFruit = [Fruit]::$UsrString
switch ($myFruit) {
Apple, Orange {
Write-Host "This is an apple or an orange"
break
}
Banana {
Write-Host "This is a banana"
break
}
default {
Write-Host "Unknown fruit: $myFruit"
break
}
}
But each attempt I have made has yet to work as intended, if at all.
I have searched for anything to do with Powershell, switch-statements, and enums but have yet to find the answer.
I have reviewed this link on StackOverflow.
I have successfully used the "switch -regEx (someEnum)" as long as I use the integer values of the enum items. However, that essentially defeats the use of the enum (ie. labeled ints).
My intent was to keep the switch as efficient as possible (ie. switching on simple int values; staying away from the more common string-compare style PS switch usage, just to keep performance up).
Does the multi-item case block syntax exist for enum usage in powershell?
Thanks in advance for any information.
答案1
得分: 2
switch语句:
因此:
switch ($myFruit) {
{ $_ -in 'Apple', 'Orange' } {
"这是苹果或橙子"
break
}
Banana {
"这是香蕉"
break
}
default {
"未知水果: $_"
}
}
注意:
-
如您在上面的
default分支的 动作 脚本块中所看到的,$_也在那里引用输入值。-
通常情况下,使用
$_更可取,因为switch的输入值可以是 数组(类似列表的集合),在这种情况下,分支会对每个元素进行评估(然后$_引用该元素)。 -
对于作为输入的数组,请在动作脚本块中使用
continue代替break以继续处理 下一个元素(break将阻止进一步处理其他元素)。
-
-
请注意,即使
[Fruit]枚举值与 字符串 进行比较,比较也能正常工作,因为 PowerShell 方便地自动将枚举值转换为其字符串表示形式,反之亦然。
英文:
The switch statement:
-
does not support multiple branch conditions, but ...
-
does support using a script block (
{ ... }) as a branch condition, inside of which you're free to perform any test, including for multiple values, using the automatic$_variable to refer to the input value at hand.
Therefore:
switch ($myFruit) {
{ $_ -in 'Apple', 'Orange' } {
"This is an apple or an orange"
break
}
Banana {
"This is a banana"
break
}
default {
"Unknown fruit: $_"
}
}
Note:
-
As you can see in the
defaultbranch's action script block above,$_also refers to the input value there.-
Using
$_is generally preferable, as theswitchinput value can be an array (list-like collection), in which case the branches are evaluated for each element (and$_then refers to the element at hand. -
With an array as input, use
continuein lieu ofbreakin an action script block in order to continue processing with the next element (breakwill prevent processing of further elements).
-
-
Note that even though the
[Fruit]enumeration values are compared against strings, the comparison works, because PowerShell conveniently automatically converts enumeration values to and from their string representations.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论