What is the best way to convert a List to a Hashtable.
Say I have a list like
("Key",$value,"Key2",$value2)
What is the shortest syntax to convert it into a Hashtable?
What is the best way to convert a List to a Hashtable.
Say I have a list like
("Key",$value,"Key2",$value2)
What is the shortest syntax to convert it into a Hashtable?
Try the following
$table = new-object System.Collections.Hashtable
for ( $i = 0; $i -lt $list.Length; $i += 2 ) {
$table.Add($list[$i],$list[$i+1]);
}
How about:
$ht = @{}
$key = "";
("Key",5,"Key2",6) | foreach `
{
if($key)
{
$ht.$key = $_;
$key="";
} else
{$key=$_}
}
Function ConvertTo-Hashtable($list) {
$h = @{}
while($list) {
$head, $next, $list = $list
$h.$head = $next
}
$h
}
ConvertTo-Hashtable ("Key",1,"Key2",2)