【发布时间】:2021-10-07 23:11:01
【问题描述】:
我对 Powershell 5.1 中的三元运算符有疑问。如果我尝试分配数组 @(...):
,它不会像我预期的那样工作 # I can assign an array to a variable:
$a=@("TRUE")
write-host "a:" $a
# I can do that in a proper if/then statement, too:
$b = @(if ($false) {@("True")} else {@("False")})
write-host "b:" $b
# Assignment of string constants works fine with ternary syntax:
$c = @( {"True"} , {"False"} )[!$true]
write-host "c:" $c
# but not with array assignments:
$d = @( {@("True")} , {@("False")} )[!$false]
write-host "d:" $d
# Expected: "False".
# Actual output: @("False")
输出:
a: TRUE
b: False
c: "True"
d: @("False")
关于三元运算符的线程并没有为我澄清这一点。 (Ternary operator in PowerShell)
更新: $d 需要是一个数组,以及两个内部项。为简单起见,这里只显示了一个字符串常量。
【问题讨论】:
标签: powershell conditional-operator ternary