Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function powershell函数+;词典_Function_Powershell_Dictionary - Fatal编程技术网

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