散列哈希的PowerShell散列:如何填充它们?

时间:2014-06-24 15:28:11

标签: powershell hash

最终情况应如下:

   $BaseHash = @{
       "KEY_A" = @{
               "Hash2" = @{ "Txt"="descr"; "Val"=1.2;"End"=(get-date) }
               "Hash3" = @{ "Txt"="descr"; "Val"=1.2;"End"=(get-date) }
        }
       "KEY_B" = @{
               "Hash2" = @{ "Txt"="descr"; "Val"=1.2;"End"=(get-date) }
               "Hash3" = @{ "Txt"="descr"; "Val"=1.2;"End"=(get-date) }
        }
       "KEY_C" = @{
               "Hash2" = @{ "Txt"="descr"; "Val"=1.2;"End"=(get-date) }
               "Hash3" = @{ "Txt"="descr"; "Val"=1.2;"End"=(get-date) }
        }
   }

但我开始时所有的哈希都是'空的':

   $BaseHash = @{}

好的,我试过这个得到一个错误,但似乎有用吗?:

    $key1 = "KEY_A"
    $key2 = "KEY_B"
    $Topic= "Hash1"
    $Topic2= "Hash2"
    $defHash  = @{ "Txt"="Descr.";"Val"=1.2;"end"=(get-date) }
    $empty    = @{}
    $BaseHash = @{}
    if ( !$BaseHash.ContainsKey($key1) ) { 
        $BaseHash.Add( $key1, $empty  )
        $BaseHash.$key1.Add( $Topic, $defHash )
        $BaseHash.$key1.Add( $Topic2, $defHash )
    }
    if ( !$BaseHash.ContainsKey($key2) ) { 
        $BaseHash.Add( $key2, $empty )
        $BaseHash.$key2.Add( $Topic, $defHash )
        $BaseHash.$key2.Add( $Topic2, $defHash )
    }
    #$BaseHash
    foreach ($h in $BaseHash.$key1.GetEnumerator() | select -ExpandProperty name) {
      if ( $BaseHash.$key1.$h.Val -ne 0.0 ) { 
        Write-Host "Found $key1  $h  $($BaseHash.$key1.$h.Val) $($BaseHash.$key1.$h.Txt)"
      }
    }
    foreach ($h in $BaseHash.$key2.GetEnumerator() | select -ExpandProperty name) {
      if ( $BaseHash.$key2.$h.Val -ne 0.0 ) { 
        Write-Host "Found $key2  $h  $($BaseHash.$key2.$h.Val) $($BaseHash.$key2.$h.Txt)"
      }
    }

我得到的错误是将主题和Topic2添加到key2-hash:
调用Add with 2 Arguments时的异常:t元素已被添加:

+ $BaseHash.$key2.Add <<<< ( $Topic, $defHash )<br>
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException<br>
+ FullyQualifiedErrorId : DotNetMethodException<br>

+ $BaseHash.$key2.Add <<<< ( $Topic2, $defHash )<br>
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException<br>
+ FullyQualifiedErrorId : DotNetMethodException<br>

另一方面,所有似乎都添加了:

找到KEY_A Hash2 1.2描述 找到KEY_A Hash1 1.2描述 找到KEY_B Hash2 1.2描述 找到KEY_B Hash1 1.2描述。

1)如何避免错误信息?
2)你能建议一个更好的方法来向哈希添加哈希哈希吗?

提前致谢
Gooly

2 个答案:

答案 0 :(得分:2)

我发现了这个问题。问题是您使用$ empty作为空白哈希表,但随后在另一个哈希表中使用它,并尝试稍后重新使用它。在您Add( $key1, $empty )的位置,您已将哈希表设置为值。当您调用$key1.Add()时,您正在执行$ empty哈希表的.Add()方法,向其中添加成员。当您使用$ key2执行任何操作时,$ empty散列表已经填充了$ topic和$ topic2,因此当您尝试再次添加它时会出现错误。

相反,只需执行$BaseHash.Add( $Key1, @{} )并从那里开始。

答案 1 :(得分:1)

如下:

$BaseHash = @{}

$key1 = "KEY_A"
$key2 = "KEY_B"
$Topic= "Hash1"
$Topic2= "Hash2"
$defHash  = @{ "Txt"="Descr.";"Val"=1.2;"end"=(get-date) }

if( -not $BaseHash.ContainsKey( $key1 )) {
    $BaseHash[$key1] = [ordered]@{}
    $BaseHash[$key1][$Topic] += $defHash
    $BaseHash[$key1][$Topic2] += $defHash
}

if( -not $BaseHash.ContainsKey( $key2 )) {
    $BaseHash[$key2] = [ordered]@{}
    $BaseHash[$key2][$Topic] += $defHash
    $BaseHash[$key2][$Topic2] += $defHash
}

foreach ($h in $BaseHash.$key1.GetEnumerator() | select -ExpandProperty name) {
      if ( $BaseHash.$key1.$h.Val -ne 0.0 ) { 
        Write-Host "Found $key1  $h  $($BaseHash.$key1.$h.Val) $($BaseHash.$key1.$h.Txt)"
      }
    }
    foreach ($h in $BaseHash.$key2.GetEnumerator() | select -ExpandProperty name) {
      if ( $BaseHash.$key2.$h.Val -ne 0.0 ) { 
        Write-Host "Found $key2  $h  $($BaseHash.$key2.$h.Val) $($BaseHash.$key2.$h.Txt)"
      }
    }