gpt4 book ai didi

powershell - 哈希表和键顺序

转载 作者:行者123 更新时间:2023-12-03 09:17:22 26 4
gpt4 key购买 nike

有没有办法在添加键时将键的顺序保持在哈希表中?就像推送/弹出机制。

例:

$hashtable = @{}

$hashtable.Add("Switzerland", "Bern")
$hashtable.Add("Spain", "Madrid")
$hashtable.Add("Italy", "Rome")
$hashtable.Add("Germany", "Berlin")
$hashtable

我想保留将元素添加到哈希表的顺序。

最佳答案

PowerShell V1 / V2中没有内置解决方案。您将要使用.NET
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

关于powershell - 哈希表和键顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14891044/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com