Function powershell函数+;词典
我不理解这种行为:Function powershell函数+;词典,function,powershell,dictionary,Function,Powershell,Dictionary,我不理解这种行为: function foo ($item, $dict_of_item) { if ($dict_of_item.keys -contains $item){ return $dict_of_item[$item] } else{ return $item } } $dict = @{ 'a' = 1 'b' = 2 'c' = 3} foo('a',$dict) a Name
function foo ($item, $dict_of_item) {
if ($dict_of_item.keys -contains $item){
return $dict_of_item[$item] }
else{
return $item }
}
$dict = @{
'a' = 1
'b' = 2
'c' = 3}
foo('a',$dict)
a
Name Value
---- -----
c 3
b 2
a 1
我想要char的返回码,但我不明白为什么我在输出中得到dictionary。您调用的函数不正确。您不会在参数周围加括号,而是像命令行参数一样传递它们。因此,在你的情况下,改变:
foo('a',$dict)
致:
或者更清楚地说:
foo -item 'a' -dict_of_item $dict
PowerShell将('a',$dict)
解释为一个数组,因此您有:
$item = ('a',$dict)
$dict_of_item = $null
由于您的测试因这些参数而失败,它将向您返回$item
。也就是说,它返回一个带有字符和字典的数组,这就是您在屏幕上看到的
$item = ('a',$dict)
$dict_of_item = $null