有没有办法在添加键时保持哈希表中键的顺序?就像推/弹出机制一样。
Example:
$hashtable = @{}
$hashtable.Add("Switzerland", "Bern")
$hashtable.Add("Spain", "Madrid")
$hashtable.Add("Italy", "Rome")
$hashtable.Add("Germany", "Berlin")
$hashtable
我想保留将元素添加到哈希表的顺序。
PowerShell V1 / V2 中没有内置解决方案。您将需要使用 .NETSystem.Collections.Specialized.OrderedDictionary https://learn.microsoft.com/en-us/dotnet/api/system.collections.specialized.ordereddictionary:
$order = New-Object System.Collections.Specialized.OrderedDictionary
$order.Add("Switzerland", "Bern")
$order.Add("Spain", "Madrid")
$order.Add("Italy", "Rome")
$order.Add("Germany", "Berlin")
PS> $order
Name Value
---- -----
Switzerland Bern
Spain Madrid
Italy Rome
Germany Berlin
在 PowerShell V3 中,您可以转换为 [ordered]:
PS> [ordered]@{"Switzerland"="Bern"; "Spain"="Madrid"; "Italy"="Rome"; "Germany"="Berlin"}
Name Value
---- -----
Switzerland Bern
Spain Madrid
Italy Rome
Germany Berlin
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)