Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Hash Tables Double Key Error: "a" and "A"

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

like image 657
gooly Avatar asked Jun 05 '14 07:06

gooly


People also ask

How do I remove a key from a Hashtable in PowerShell?

To remove the Key-value from the Hashtable, you need to use the Remove(Key) method. You cannot remove the hashtable entry with the values. You must use the key inside the Remove() method.

Can Hashtable have duplicate keys?

Actually a Map (whether it is a HashTable, HashMap or whatever) can have duplicate values, but not duplicate keys.

How do I get key-value pairs in PowerShell?

The key/value pairs might appear in a different order each time that you display them. Although you cannot sort a hash table, you can use the GetEnumerator method of hash tables to enumerate the keys and values, and then use the Sort-Object cmdlet to sort the enumerated values for display.

How do I merge two hash tables in PowerShell?

Adding values of the hash table is simple as the adding string. We just need to use the addition operator (+) to merge two hash table values.


2 Answers

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
like image 90
Oscar Foley Avatar answered Nov 16 '22 01:11

Oscar Foley


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"
like image 43
Mohammad Nadeem Avatar answered Nov 16 '22 00:11

Mohammad Nadeem