回答
請嘗試以下
$table = new-object System.Collections.Hashtable
for ($i = 0; $i -lt $list.Length; $i += 2) {
$table.Add($list[$i],$list[$i+1]);
}
我想我希望有一些更簡潔的東西 –
如何:
$ht = @{}
$key = "";
("Key",5,"Key2",6) | foreach `
{
if($key)
{
$ht.$key = $_;
$key="";
} else
{$key=$_}
}
我喜歡這一點,但你可以簡化一下: $ ht = @ {}; $ key = 0 「Key」,5,「Key2」,6 | foreach {if($ key){$ ht。$ key = $ _; $ key = 0} else {$ key = $ _}} –
注意,註釋格式化程序在「Key」之前吃掉了我的換行符。 。 –
謝謝。我想這是少打字。 – zdan
$h = @{}
0..($l.count - 1) | ? {$_ -band 1} | % {$h.Add($l[$_-1],$l[$_])}
$h = @{}
0..($l.count - 1) | ? {$_ -band 1} | % {$h.($l[$_-1]) = $l[$_]}
$h = @{}
$i = 0
while ($i -lt $l.count) {$h.Add($l[$i++],$l[$i++])}
Function ConvertTo-Hashtable($list) {
$h = @{}
while($list) {
$head, $next, $list = $list
$h.$head = $next
}
$h
}
ConvertTo-Hashtable ("Key",1,"Key2",2)
另請參閱Bruce Payette的文章:[PowerShell提示:如何「移動」陣列...](http://blogs.msdn.com/b/powershell/archive/2007/02/06/powershell-tip-how -to-shift-arrays.aspx) –
是的。此外,這是功能世界的標準模式。 可以縮短到 而($頭,$下,$列表= $列表){$ 小時。$頭= $下 } –
洛夫該溶液中。 – craig
如果您KeyValuePairs,是明確 'Microsoft.Azure.Management.WebSites.Models.NameValuePair',那麼你可以使用:
Function ConvertListOfNVPTo-Hashtable($list) {
$h = @{}
$list | ForEach-Object {
$ht[$_.Name] = $_.Value
}
return $h
}
- 1. 轉換表列鍵值對
- 2. 將鍵/值對列表轉換爲數據表
- 3. 將var數據轉換爲散列表
- 4. 防止將散列表轉換爲ASCII
- 5. 將PSCustomObject轉換爲散列表
- 6. django將對象列表轉換爲主鍵列表
- 7. 將對象列表轉換爲散列圖
- 8. Clojure:將字符串轉換爲散列圖中的鍵值對
- 9. 將Varchar值轉換爲散列值:varchar
- 10. 將散列轉換爲嵌套散列
- 11. 將分層文本列表轉換爲離散列表
- 12. 如何使用`AngularJS`將鍵值對轉換爲數組列表?
- 13. 將鍵值對轉換爲「衣衫襤褸」列表
- 14. 將錶轉換爲值列表
- 15. 將dict轉換爲鍵列表?
- 16. 如何將內容從以前的散列表轉換爲新的散列表?
- 17. 將散列轉換爲NSString?
- 18. 將值列表轉換爲CSV值
- 19. 將字符串中的值轉換爲散列表?
- 20. 如何將散列表的值轉換爲字符串i Java
- 21. 如何將散列表向下轉換爲抽象類對象
- 22. 如何將散列數組轉換爲散列值數組?
- 23. Rails將類似散列的對象轉換爲散列?
- 24. 將列表轉換爲列表清單
- 25. 將列表轉換爲鏈接列表
- 26. 將列表從ord轉換爲列表
- 27. 將列表轉換爲字典列表
- 28. 將列表轉換爲一個列表
- 29. 將列表轉換爲整數列表
- 30. 將元組列表轉換爲列表?
在Perl中更容易,不是嗎? :) – Ivan