powershell - How to specify data type for hashtable? -
it seems powershell hashtable (@{}
) map of string→string default. wish value type int32
calculation on it.
how specify type information when declaring hashtable variable?
hashtables map keys values. type of keys , values immaterial.
ps c:\> $ht = @{} ps c:\> $ht[1] = 'foo' ps c:\> $ht['2'] = 42 ps c:\> $ht name value ---- ----- 2 42 1 foo ps c:\> $fmt = "{0} [{1}]`t-> {2} [{3}]" ps c:\> $ht.keys | % {$fmt -f $_, $_.gettype().name, $ht[$_], $ht[$_].gettype().name} 2 [string] -> 42 [int32] 1 [int32] -> foo [string]
if have integer in string , want assign integer, can cast on assignment:
ps c:\> $ht[3] = [int]'23' ps c:\> $ht.keys | % {$fmt -f $_, $_.gettype().name, $ht[$_], $ht[$_].gettype().name} 2 [string] -> 42 [int32] 3 [int32] -> 23 [int32] 1 [int32] -> foo [string]
Comments
Post a Comment