hax*_*ode 5 windows powershell hashmap
在 powershell 中,如何创建一个哈希图,其中包含一个字符串作为键和一个字符串列表作为值?
例如,在 Java 中,可以执行以下操作:
Map<String, List<String> myMap = new HashMap<String, List<String>();
powershell 是否包含此功能?
我试过了:
$myMap = @{ [string], New-Object Collections.Generic.List[string] } 
但这没有用。
$myMap = @{ "Michigan"="Detroit"; "California" = "Sacremento","Hollywood";"Texas"="Austin" } 
请注意 California 如何为其键分配两个值。另请注意,如果您尝试将其导出到 csv,您将如何遇到麻烦,枚举器会跳过数组值并仅将“System.Object[]”插入到 csv 文件中,这可能是应该的。
更新
要动态更新列表,
$myMap.California += "Compton"
我想到了。
$myList = New-Object Collections.Generic.List[string]
$myList.Add("one")
$myList.Add("two")
$myList.Add("three")
$myHash = @{ }
$myHash.Add("list", $myList)