排序哈希表并放入新的哈希表

时间:2018-06-19 14:32:34

标签: powershell hashtable enumeration

我不知道为什么下面的循环返回空键。 我认为$ hashSorted不是Hashtable类型;所以我怎么强迫它成为? 现在想我需要深入/副本吗?我只想对我的哈希表进行排序,然后将其放入另一个哈希表。

这可能是以下项的重复项:Sort Hashtable and assign it to a new variable in Powershell   我最初的问题是为什么循环时键为空。   然后我意识到这可能不是哈希表。仍在尝试答案。

参考:How can I enumerate a hashtable as key-value pairs / filter a hashtable by a collection of key values

cls
$hashFilesAndSizes = @{}
$hashSorted = @{}
$hashFilesAndSizes.Add("file1.txt",1000)
$hashFilesAndSizes.Add("file2.txt",200)
$hashFilesAndSizes.Add("file3.txt",750)
$hashFilesAndSizes.GetEnumerator() | sort value  #Displays sorted hash table 

Write-Host "Second Try - put in another hashtable"
$hashSorted = $hashFilesAndSizes.GetEnumerator() | sort value  #put into a new variable 
Write-Host "Original variable in original Order" 
$hashFilesAndSizes
Write-Host "Sorted" 
$hashSorted #show results 

Write-Host "Why loop has null keys?"

  foreach($key in $hashSorted.Keys) 
   {
      Write-Host "Key=$key" 
      #if (-not ([string]::IsNullOrEmpty($key)))
      #{
          $keyPadded = $key.PadRight(50," ")
          $fileSize = $hashSorted[$key] 
          $fileSizeFormatted = $fileSize.ToString("000,000")
          Write-Host "$keyPadded  size=$fileSizeFormatted "
      #}
   }

Write-Host "Test with enumerator"

  foreach($item in $hashSorted.GetEnumerator()) 
   {
      $key = $hashSorted.Key 
      Write-Host "Key=$key" 
      #if (-not ([string]::IsNullOrEmpty($key)))
      #{
          $keyPadded = $key.PadRight(50," ")
          $fileSize = $hashSorted.Value
          $fileSizeFormatted = $fileSize.ToString("000,000")
          Write-Host "$keyPadded  size=$fileSizeFormatted "
      #}
   }

结果:

> Name                           Value                                  
> 
> ----                           -----                                                                                                                     file2.txt                      200                                    
> file3.txt                      750                                    
> file1.txt                      1000                                   
> Second Try - put in another hashtable Original variable in original
> Order file3.txt                      750                              
> file1.txt                      1000                                   
> file2.txt                      200                                    
> Sorted file2.txt                      200                             
> file3.txt                      750                                    
> file1.txt                      1000                                   
> Why loop has null keys? Key= You cannot call a method on a null-valued
> expression. At C:\Scripts\HashTableSortTest.ps1:23 char:37
> +           $keyPadded = $key.PadRight <<<< (50," ")
>     + CategoryInfo          : InvalidOperation: (PadRight:String) [], RuntimeException
>     + FullyQualifiedErrorId : InvokeMethodOnNull   Index operation failed; the array index evaluated to null. At
> C:\Scripts\HashTableSortTest.ps1:24 char:35
> +           $fileSize = $hashSorted[ <<<< $key] 
>     + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
>     + FullyQualifiedErrorId : NullArrayIndex   You cannot call a method on a null-valued expression. At
> C:\Scripts\HashTableSortTest.ps1:25 char:50
> +           $fileSizeFormatted = $fileSize.ToString <<<< ("000,000")
>     + CategoryInfo          : InvalidOperation: (ToString:String) [], RuntimeException
>     + FullyQualifiedErrorId : InvokeMethodOnNull
>     size=  
>Test with enumerator Key= You cannot call a method on a null-valued expression. At C:\Scripts\HashTableSortTest.ps1:38 char:37
> +           $keyPadded = $key.PadRight <<<< (50," ")
>     + CategoryInfo          : InvalidOperation: (PadRight:String) [], RuntimeException
>     + FullyQualifiedErrorId : InvokeMethodOnNull   You cannot call a method on a null-valued expression. At
> C:\Scripts\HashTableSortTest.ps1:40 char:50
> +           $fileSizeFormatted = $fileSize.ToString <<<< ("000,000")
>     + CategoryInfo          : InvalidOperation: (ToString:String) [], RuntimeException
>     + FullyQualifiedErrorId : InvokeMethodOnNull
>     size=  Key= You cannot call a method on a null-valued expression. At C:\Scripts\HashTableSortTest.ps1:38 char:37
> +           $keyPadded = $key.PadRight <<<< (50," ")
>     + CategoryInfo          : InvalidOperation: (PadRight:String) [], RuntimeException
>     + FullyQualifiedErrorId : InvokeMethodOnNull   You cannot call a method on a null-valued expression. At
> C:\Scripts\HashTableSortTest.ps1:40 char:50
> +           $fileSizeFormatted = $fileSize.ToString <<<< ("000,000")
>     + CategoryInfo          : InvalidOperation: (ToString:String) [], RuntimeException
>     + FullyQualifiedErrorId : InvokeMethodOnNull
>     size=  Key= You cannot call a method on a null-valued expression. At C:\Scripts\HashTableSortTest.ps1:38 char:37
> +           $keyPadded = $key.PadRight <<<< (50," ")
>     + CategoryInfo          : InvalidOperation: (PadRight:String) [], RuntimeException
>     + FullyQualifiedErrorId : InvokeMethodOnNull   You cannot call a method on a null-valued expression. At
> C:\Scripts\HashTableSortTest.ps1:40 char:50
> +           $fileSizeFormatted = $fileSize.ToString <<<< ("000,000")
>     + CategoryInfo          : InvalidOperation: (ToString:String) [], RuntimeException
>     + FullyQualifiedErrorId : InvokeMethodOnNull
>     size=

3 个答案:

答案 0 :(得分:3)

$hashSorted = $hashFilesAndSizes.GetEnumerator() | sort value 

不会生成新的哈希表。相反,它将产生DictionaryEntry对象的数组。现在,由于$hashSorted实际上不是哈希表,因此没有Keys属性,并且按值索引将不起作用。要正确构造一个保留键顺序的新哈希表,您必须要做的

$hashSorted = [ordered] @{}
$hashFilesAndSizes.GetEnumerator() | sort value | foreach {$hashSorted[$_.Key] = $_.Value}

答案 1 :(得分:1)

尝试在新的清晰Powershell上下文中运行。我建议您的环境在缓存中保存一些变量。

我看到的唯一错误是在第二个循环中。您在循环内使用的是<div class="container container-margin"> <div class="columns"> <div class="column is-one-third"> <div class="card"> <div class="card-image"> <figure class="image"> <img class="bw-filter" src="/images/battleport.png"/> </figure> </div> <div class="card-content remove-padding-left"> <div class="content"> <h6 class="">Battleport</h6> <p><i>Study</i></p> <br> </div> </div> </div> </div> <div class="column is-one-third"> <div class="card"> <div class="card-image"> <figure class="image"> <img class="bw-filter" src="/images/crmfabriek.png"/> </figure> </div> <div class="card-content remove-padding-left"> <div class="content"> <h6 class="">CRM Fabriek</h6> <p><i>Business</i></p> <br> </div> </div> </div> </div> <div class="column is-one-thrid"> </div> </div> ,而不是$hashSorted。应该是:

$item

编辑:顺便说一句,使用GetEnumerator()进行排序就可以了。

编辑2: $key = $item.Key .... $fileSize = $item.Value 为空,因为$hashSorted.Keys不是哈希表,而是键/值对的集合。

答案 2 :(得分:0)

Write-Host "Test with enumerator"

  foreach($item in $hashSorted.GetEnumerator()) 
   {
      $key = $item.Key                                        # Not $hashSorted.Key !
      Write-Host "Key=$key" 
      #if (-not ([string]::IsNullOrEmpty($key)))
      #{
          $keyPadded = $key.PadRight(50," ")
          $fileSize = $item.Value                             # Not $hashSorted.Value !
          $fileSizeFormatted = $fileSize.ToString("000,000")
          Write-Host "$keyPadded  size=$fileSizeFormatted "
      #}
   }