PowerShell Hash Tables Double Key Error: “a” and “A”

╄→гoц情女王★ 提交于 2020-07-08 05:33:13

问题


from another application I have key-value pairs which I want to use in my script.
But there they have e.g. the keys "a" and "A" - which causes an Error double keys aren't allowed.

  $x = @{ "a" = "Entry for a"; "A" = "S.th.else for A" }

What can I do as I would need both or none?

Thanks in advance,
Gooly


回答1:


By default PowerShell Hash tables are not case sensitive. Try this

$h = new-object System.Collections.Hashtable
$h['a'] = "Entry for a"
$h['A'] = "S.th.else for A"
$h[0] = "Entry for 0"
$h[1] = "Entry for 1"
$h

Or this (depending on the syntax that you prefer)

$hash = New-Object system.collections.hashtable
$hash.a = "Entry for a"
$hash.A = "S.th.else for A"
$hash.0 = "Entry for 0"
$hash.1 = "Entry for 1"
$hash.KEY
$hash



回答2:


You can create a case-sensitive powerhsell hash table using System.Collections.Hashtable

$x = New-Object System.Collections.Hashtable 
$x['a']="Entry for a"
$x['A']="S.th.else for A"


来源:https://stackoverflow.com/questions/24054147/powershell-hash-tables-double-key-error-a-and-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!