Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a hashmap that contains strings and a list in powershell?

In powershell how does one create a hashmap that contains a string as the key and a list of strings as the values?

For example, in Java, the following can be done:

Map<String, List<String> myMap = new HashMap<String, List<String>();

Does powershell contain this capability?

I've tried:

$myMap = @{ [string], New-Object Collections.Generic.List[string] } 

But this didn't work.

like image 276
hax0r_n_code Avatar asked Oct 08 '13 14:10

hax0r_n_code


Video Answer


2 Answers

$myMap = @{ "Michigan"="Detroit"; "California" = "Sacremento","Hollywood";"Texas"="Austin" } 

Note how California has two values assigned to it's key. Also note how you will run into trouble if you ever try to export this to csv, the enumerator skips over the array value and just inserts "System.Object[]" into the csv file, probably as it should.

Update

To dynamically update the list,

$myMap.California += "Compton"
like image 102
MDMoore313 Avatar answered Sep 30 '22 11:09

MDMoore313


I figured it out.

$myList = New-Object Collections.Generic.List[string]
$myList.Add("one")
$myList.Add("two")
$myList.Add("three")

$myHash = @{ }
$myHash.Add("list", $myList)
like image 20
hax0r_n_code Avatar answered Sep 30 '22 12:09

hax0r_n_code