如何从嵌套的哈希表值中添加值
How to add values from nested hashTable values
下面是我的代码。我想添加然后读取单个值。
$ht = @{
'Hcohesity01' = @{
'Audit' = 1
'Block' = 2
'Change' = 3
'percentage' = @{
'server1' = 4
'server2' = 5
'server3' = 10
}
}
'Hcohesity02' = @{
'Audit' = 1
'Block' = 2
'Change' = 3
'percentage' = @{
'server1' = 4
'server2' = 5
'server3' = 10
}
}
}
$ht['Hcohesity02']['percentage']['server4'] = 20
foreach ( $value -in $ht['Hcohesity02']['percentage'].Values){
$server5 += $value
}
$ht['Hcohesity02']['percentage']['server5'] =$server5
$ht['Hcohesity02']['percentage']
下面的代码不起作用,知道吗?
foreach ( $value -in $ht['Hcohesity02']['percentage'].Values)
不太清楚你要做什么。如果它只是遍历你的 foreach 循环并分配一个值,你需要去掉“-in”上的破折号。它应该如下所示:
foreach ($value in $ht.Hcohesity02.percentage.Values)
{
# do some stuff
}
代码中唯一真正的错误是在 foreach 循环中写入 -in
。那应该只是 in
.
您可以使用 one-liner 和 Measure-Object
:
而不是使用循环 add-up 值
$ht = @{
'Hcohesity01' = @{
'Audit' = 1
'Block' = 2
'Change' = 3
'percentage' = @{
'server1' = 4
'server2' = 5
'server3' = 10
}
}
'Hcohesity02' = @{
'Audit' = 1
'Block' = 2
'Change' = 3
'percentage' = @{
'server1' = 4
'server2' = 5
'server3' = 10
}
}
}
$ht['Hcohesity02']['percentage']['server4'] = 20
# instead of a foreach loop:
# $server5 = 0 # initialize
# foreach ( $value in $ht['Hcohesity02']['percentage'].Values) {
# $server5 += $value
# }
# you can do this:
$server5 = ($ht['Hcohesity02']['percentage'].Values | Measure-Object -Sum).Sum
$ht['Hcohesity02']['percentage']['server5'] = $server5
$ht['Hcohesity02']['percentage']
结果:
Name Value
---- -----
server2 5
server5 39
server3 10
server1 4
server4 20
如果您不喜欢使用 [property]
语法处理属性,您也可以使用点符号,如 在他的回答中所示:
$ht.'Hcohesity02'.'percentage'.Values # and so on
下面是我的代码。我想添加然后读取单个值。
$ht = @{
'Hcohesity01' = @{
'Audit' = 1
'Block' = 2
'Change' = 3
'percentage' = @{
'server1' = 4
'server2' = 5
'server3' = 10
}
}
'Hcohesity02' = @{
'Audit' = 1
'Block' = 2
'Change' = 3
'percentage' = @{
'server1' = 4
'server2' = 5
'server3' = 10
}
}
}
$ht['Hcohesity02']['percentage']['server4'] = 20
foreach ( $value -in $ht['Hcohesity02']['percentage'].Values){
$server5 += $value
}
$ht['Hcohesity02']['percentage']['server5'] =$server5
$ht['Hcohesity02']['percentage']
下面的代码不起作用,知道吗? foreach ( $value -in $ht['Hcohesity02']['percentage'].Values)
不太清楚你要做什么。如果它只是遍历你的 foreach 循环并分配一个值,你需要去掉“-in”上的破折号。它应该如下所示:
foreach ($value in $ht.Hcohesity02.percentage.Values)
{
# do some stuff
}
代码中唯一真正的错误是在 foreach 循环中写入 -in
。那应该只是 in
.
您可以使用 one-liner 和 Measure-Object
:
$ht = @{
'Hcohesity01' = @{
'Audit' = 1
'Block' = 2
'Change' = 3
'percentage' = @{
'server1' = 4
'server2' = 5
'server3' = 10
}
}
'Hcohesity02' = @{
'Audit' = 1
'Block' = 2
'Change' = 3
'percentage' = @{
'server1' = 4
'server2' = 5
'server3' = 10
}
}
}
$ht['Hcohesity02']['percentage']['server4'] = 20
# instead of a foreach loop:
# $server5 = 0 # initialize
# foreach ( $value in $ht['Hcohesity02']['percentage'].Values) {
# $server5 += $value
# }
# you can do this:
$server5 = ($ht['Hcohesity02']['percentage'].Values | Measure-Object -Sum).Sum
$ht['Hcohesity02']['percentage']['server5'] = $server5
$ht['Hcohesity02']['percentage']
结果:
Name Value
---- -----
server2 5
server5 39
server3 10
server1 4
server4 20
如果您不喜欢使用 [property]
语法处理属性,您也可以使用点符号,如
$ht.'Hcohesity02'.'percentage'.Values # and so on