Powershell 对哈希表进行排序并放入新的哈希表中
我不明白为什么下面的循环返回空键。 我认为$hashSorted不是Hashtable类型;那么我该如何强迫它呢? 现在想我需要一份深度/副本吗?我只想对我的哈希表进行排序,并将其放入另一个哈希表中 这可能是重复的: 我最初的问题是为什么循环时键为空。 然后我意识到它可能不是哈希表。还在那里尝试答案 参考: 结果:Powershell 对哈希表进行排序并放入新的哈希表中,powershell,hashtable,enumeration,Powershell,Hashtable,Enumeration,我不明白为什么下面的循环返回空键。 我认为$hashSorted不是Hashtable类型;那么我该如何强迫它呢? 现在想我需要一份深度/副本吗?我只想对我的哈希表进行排序,并将其放入另一个哈希表中 这可能是重复的: 我最初的问题是为什么循环时键为空。 然后我意识到它可能不是哈希表。还在那里尝试答案 参考: 结果: > Name Value > > ----
> 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=
>名称值
>
>----file2.txt 200
>file3.txt 750
>file1.txt 1000
>第二次尝试-在Original中放入另一个哈希表原始变量
>订单文件3.txt 750
>file1.txt 1000
>file2.txt 200
>排序文件2.txt 200
>file3.txt 750
>file1.txt 1000
>为什么循环有空键?Key=不能对空值函数调用方法
>表情。在C:\Scripts\HashTableSortTest.ps1:23 char:37
>+$keyPadded=$key.PadRight C:\Scripts\HashTableSortTest.ps1:24 char:35
>+$fileSize=$HASHORDED[C:\Scripts\HashTableSortTest.ps1:25 char:50
>+$FILESIZEFORMATED=$fileSize.ToString size=
>Test with enumerator Key=不能对空值表达式调用方法。位于C:\Scripts\HashTableSortTest.ps1:38 char:37
>+$keyPadded=$key.PadRight C:\Scripts\HashTableSortTest.ps1:40 char:50
>+$FILESIZEFORMATED=$fileSize.ToString size=Key=不能对空值表达式调用方法。位于C:\Scripts\HashTableSortTest.ps1:38 char:37
>+$keyPadded=$key.PadRight C:\Scripts\HashTableSortTest.ps1:40 char:50
>+$FILESIZEFORMATED=$fileSize.ToString size=Key=不能对空值表达式调用方法。位于C:\Scripts\HashTableSortTest.ps1:38 char:37
>+$keyPadded=$key.PadRight C:\Scripts\HashTableSortTest.ps1:40 char:50
>+$FILESIZEFORMATED=$fileSize.ToString size=
尝试在新的clear powershell上下文中运行。我建议您的环境在缓存中保存一些变量
我看到的唯一错误是在第二个循环中。您在循环中使用的是$hashSorted
,而不是$item
。应该是:
$key = $item.Key
....
$fileSize = $item.Value
编辑:顺便说一句,使用GetEnumerator()进行排序效果很好
编辑2:
$hashSorted。键
为空,因为$hashSorted
不是哈希表,只是键值对的集合。尝试在新的clear powershell上下文中运行。我建议您的环境在缓存中保存了一些变量
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 "
#}
}
我看到的唯一错误是在第二个循环中。您在循环中使用的是$hashSorted
,而不是$item
。应该是:
$key = $item.Key
....
$fileSize = $item.Value
编辑:顺便说一句,使用GetEnumerator()进行排序效果很好
编辑2:$hashSorted。键
为空,因为$hashSorted
不是哈希表,只是键值对的集合。
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 "
#}
}
不会生成新的哈希表。相反,它会生成一个字典entry
对象数组。现在由于$hashSorted
实际上不是哈希表,因此不存在键
属性,按值索引将不起作用。要正确构建保留键顺序的新哈希表,必须执行以下操作
$hashSorted = [ordered] @{}
$hashFilesAndSizes.GetEnumerator() | sort value | foreach {$hashSorted[$_.Key] = $_.Value}
这个
不会生成新的哈希表。相反,它会生成一个字典entry
对象数组。现在由于$hashSorted
实际上不是哈希表,因此不存在键
属性,按值索引将不起作用。要正确构建保留键顺序的新哈希表,必须执行以下操作
$hashSorted = [ordered] @{}
$hashFilesAndSizes.GetEnumerator() | sort value | foreach {$hashSorted[$_.Key] = $_.Value}
哈希表永远不能保证遵守顺序。对哈希表进行排序是毫无意义的。如果顺序很重要,请使用某种排序的有序集合。键不会排序,因为它们是针对.$hashFilesAndSizes.GetEnumerator()进行优化的不是哈希表,可以排序。哈希表永远不能保证遵守排序。对哈希表排序是无意义的。如果顺序很重要,请使用某种排序的有序集合。键不排序,因为它们是针对。$hashFilesAndSizes.GetEnumerator()优化的不是哈希表,可以排序。请参阅:-我仍然遇到空值问题。是的,一些变量没有被清除,我不明白为什么。我在脚本顶部初始化哈希表。修复了键入错误,但仍然存在空值问题。好的,相同的键入错误两次。我想使用GetNumerator可以工作,但循环键不行。看到另一个post这似乎表明了这一点。我在fresh powershell实例中运行了您的脚本,得到了预期的结果-根本没有空值。请参阅:-我仍然遇到空值问题。是的,一些变量没有被清除,我不明白为什么。我在脚本顶部初始化了哈希表。修复了错误,但仍然存在空值问题。好的,相同的错误两次。我猜使用GetNumerator是可行的,但循环键却不行。看到另一篇文章似乎表明了这一点。我在新的powershell实例中运行了您的脚本,并得到了预期的结果-根本没有空值。这是一个很棒的片段!这是一个很棒的片段!