如何在Powershell中比较关联数组?

发布于 2024-10-09 07:53:43 字数 165 浏览 0 评论 0原文

我有两个关联数组,

$a = @{"k1"="v1"; "k2"=@{"k21"="v21"}} 

$b = @{"k1"="v1"; "k2"=@{"k21"="v21"}} 

我想知道有没有什么好的方法可以在不编写自己的函数的情况下进行比较?

I have two associative arrays

$a = @{"k1"="v1"; "k2"=@{"k21"="v21"}} 

$b = @{"k1"="v1"; "k2"=@{"k21"="v21"}} 

I was wondering is there any good way do the comparison without writing my own function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绅刃 2024-10-16 07:53:43

除了编写一个函数来比较每个键的值(如果该值不是原始对象,则可能是递归的)之外,我不知道有什么方法。但是,PowerShell 中的关联数组只是 .NET 类型 (System.Collections.Hashtable)。您可能希望通过将 .NET 标记添加到您的问题中,向更广泛的 .NET 受众开放这个问题。

There isn't a way that I know of except writing a function to compare each key's value (potentially recursive if the value is something other than a primitive object). However, associate arrays in PowerShell are just .NET types (System.Collections.Hashtable). You might want to open this question up to the broader .NET audience by adding the .NET tag to your question.

孤者何惧 2024-10-16 07:53:43

为了完整起见,这里有一个用于比较哈希表的简单函数:

function Compare-Hashtable(
  [Hashtable]$ReferenceObject,
  [Hashtable]$DifferenceObject,
  [switch]$IncludeEqual
) {
  # Creates a result object.
  function result( [string]$side ) {
    New-Object PSObject -Property @{
      'InputPath'= "$path$key";
      'SideIndicator' = $side;
      'ReferenceValue' = $refValue;
      'DifferenceValue' = $difValue;
    }
  }

  # Recursively compares two hashtables.
  function core( [string]$path, [Hashtable]$ref, [Hashtable]$dif ) {
    # Hold on to keys from the other object that are not in the reference.
    $nonrefKeys = New-Object 'System.Collections.Generic.HashSet[string]'
    $dif.Keys | foreach { [void]$nonrefKeys.Add( $_ ) }

    # Test each key in the reference with that in the other object.
    foreach( $key in $ref.Keys ) {
      [void]$nonrefKeys.Remove( $key )
      $refValue = $ref.$key
      $difValue = $dif.$key

      if( -not $dif.ContainsKey( $key ) ) {
        result '<='
      }
      elseif( $refValue -is [hashtable] -and $difValue -is [hashtable] ) {
        core "$path$key." $refValue $difValue
      }
      elseif( $refValue -ne $difValue ) {
        result '<>'
      }
      elseif( $IncludeEqual ) {
        result '=='
      }
    }

    # Show all keys in the other object not in the reference.
    $refValue = $null
    foreach( $key in $nonrefKeys ) {
      $difValue = $dif.$key
      result '=>'
    }
  }

  core '' $ReferenceObject $DifferenceObject
}

一些示例输出:

> $a = @{ 'same'='x'; 'shared'=@{ 'same'='x'; 'different'='unlike'; 'new'='A' } }
> $b = @{ 'same'='x'; 'shared'=@{ 'same'='x'; 'different'='contrary' }; 'new'='B' }
> Compare-Hashtable $a $b

InputPath          ReferenceValue   DifferenceValue   SideIndicator
---------         --------------   ---------------   -------------
shared.different   unlike           contrary          <>
shared.new         A                                 <=
new                                 B                =>

Just for completeness, here is a simple function for comparing hashtables:

function Compare-Hashtable(
  [Hashtable]$ReferenceObject,
  [Hashtable]$DifferenceObject,
  [switch]$IncludeEqual
) {
  # Creates a result object.
  function result( [string]$side ) {
    New-Object PSObject -Property @{
      'InputPath'= "$path$key";
      'SideIndicator' = $side;
      'ReferenceValue' = $refValue;
      'DifferenceValue' = $difValue;
    }
  }

  # Recursively compares two hashtables.
  function core( [string]$path, [Hashtable]$ref, [Hashtable]$dif ) {
    # Hold on to keys from the other object that are not in the reference.
    $nonrefKeys = New-Object 'System.Collections.Generic.HashSet[string]'
    $dif.Keys | foreach { [void]$nonrefKeys.Add( $_ ) }

    # Test each key in the reference with that in the other object.
    foreach( $key in $ref.Keys ) {
      [void]$nonrefKeys.Remove( $key )
      $refValue = $ref.$key
      $difValue = $dif.$key

      if( -not $dif.ContainsKey( $key ) ) {
        result '<='
      }
      elseif( $refValue -is [hashtable] -and $difValue -is [hashtable] ) {
        core "$path$key." $refValue $difValue
      }
      elseif( $refValue -ne $difValue ) {
        result '<>'
      }
      elseif( $IncludeEqual ) {
        result '=='
      }
    }

    # Show all keys in the other object not in the reference.
    $refValue = $null
    foreach( $key in $nonrefKeys ) {
      $difValue = $dif.$key
      result '=>'
    }
  }

  core '' $ReferenceObject $DifferenceObject
}

Some example output:

> $a = @{ 'same'='x'; 'shared'=@{ 'same'='x'; 'different'='unlike'; 'new'='A' } }
> $b = @{ 'same'='x'; 'shared'=@{ 'same'='x'; 'different'='contrary' }; 'new'='B' }
> Compare-Hashtable $a $b

InputPath          ReferenceValue   DifferenceValue   SideIndicator
---------         --------------   ---------------   -------------
shared.different   unlike           contrary          <>
shared.new         A                                 <=
new                                 B                =>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文