Mar*_*yah 4 powershell powershell-4.0
我在这里有一行脚本供审查,我注意到带有值的变量声明:
function readConfig {
Param([string]$fileName)
$config = @{}
Get-Content $fileName | Where-Object {
$_ -like '*=*'
} | ForEach-Object {
$key, $value = $_ -split '\s*=\s*', 2
$config[$key] = $value
}
return $config
}
Run Code Online (Sandbox Code Playgroud)
我想知道@{}in是什么意思$config = @{}?
Ans*_*ers 10
@{} 在 PowerShell 中定义了一个哈希表,一种用于将唯一键映射到值的数据结构(在其他语言中,此数据结构称为“字典”或“关联数组”)。
@{} 它自己定义了一个空的哈希表,然后可以用值填充,例如:
$h = @{}
$h['a'] = 'foo'
$h['b'] = 'bar'
Run Code Online (Sandbox Code Playgroud)
哈希表也可以在其内容已经存在的情况下进行定义:
$h = @{
'a' = 'foo'
'b' = 'bar'
}
Run Code Online (Sandbox Code Playgroud)
但是请注意,当您在 PowerShell输出中看到类似的表示法时,例如:
美国广播公司:23
定义:@{"a"="foo";"b"="bar"}
这通常不是哈希表,而是自定义对象的字符串表示形式。
| 归档时间: |
|
| 查看次数: |
5818 次 |
| 最近记录: |