我有哈希哈希:
$h1 = @{
"h2" = @{
"key1" = 1
"key2" = 2
"key3" = 3
"key4" = 4
}
}
我可以访问并获取密钥和值:
$n = 10
ForEach($r in $h1.h2.GetEnumerator()) {
Write-Host "$($r.Name)$($r.Value)" # ok
$h1.h2.$($r.Name) = $n # Error
$n++
}
但我无法将值更改为$ n - 如何使用此循环执行此操作?
ForEach($r in $h1.h2.GetEnumerator()) {
Write-Host "$($r.Name)$($r.Value)" # want to see new values 10, 11, 12, ...
}
提前致谢, Gooly
答案 0 :(得分:1)
枚举键名,并将其用于循环枚举:
$h1 = @{
"h2" = @{
"key1" = 1
"key2" = 2
"key3" = 3
"key4" = 4
}
}
$n = 10
ForEach($r in $h1.h2.GetEnumerator() | select -ExpandProperty name) {
Write-Host "$r$($h1.h2.$r)" # ok
$h1.h2.$r = $n
$n++
}
答案 1 :(得分:0)
$h1 = @{
"h2" = @{
"key1" = 1
"key2" = 2
"key3" = 3
"key4" = 4
}
}
$n = 10
$h3 = @{}
ForEach($r in $h1.h2.Keys) {
Write-Host "$r"
$h3.Add("$($r)", $n)
$n++
}
ForEach($r in $h3.GetEnumerator()) {
Write-Host "$($r.Name)$($r.Value)" # want to see new values 10, 11, 12, ...
}