Powershell 哈希表检索问题
Powershell Hashtable Retrieval Trouble
我在使用哈希表将产品代码映射到型号名称时遇到问题:
$modelList = @{}
$key = 48525748
$value = "Dell P2217"
$modelList[$key] = $model
$key = 65486855
$value = "Dell P2217"
$modelList[$key] = $model
$key = 65486856
$value = "Dell P2217"
$modelList[$key] = $model
$key = 51505066
$value = "HP 22-incher"
$modelList[$key] = $model
write-host WHYYYY: $modelList[51505066]
write-host WHYY: $modelList.Get_Item(51505056)
上面写出来的就是WHY
.
为什么它不能检索我刚刚添加的项目?抱歉,我知道这是非常基础的,但我一辈子都弄不明白。
看起来你只是想将 $value
,而不是 $model
分配给你的哈希表条目 ,这可以解释为什么你没有输出(一个未初始化的变量在 PowerShell 中默认隐式 $null
)。
检测此类问题的一种方法是,如果您尝试获取未初始化变量的值,让 PowerShell 报告 错误,方法是 设置 Set-StrictMode -Version 1
或更高。
但是,考虑在您的情况下使用哈希表 literal,这完全不需要变量:
$modelList = @{
48525748 = "Dell P2217"
65486855 = "Dell P2218"
65486856 = "Dell P2219"
51505066 = "HP 22-incher"
}
$modelList[51505066] # -> 'HP 22-incher'
在一行中定义它也是一种选择,在这种情况下,您必须用 ;
分隔条目
$modelList = @{ 48525748 = "Dell P2217"; 65486855 = "Dell P2217"; 65486856 = "Dell P2217"; 51505066 = "HP 22-incher" }
就访问哈希表的条目而言,您有两种语法选择(除了调用参数化的.Item()
属性 / .get_Item()
方法):
索引符号:$modelList[51505066]
点符号(与对象一样):$modelList.51505066
但是,存在细微差别 - 见下文。
注意事项:
您正在使用 numbers 作为键,这里的具体类型是 [int]
(System.Int32
),PowerShell 根据值自动选择。
虽然原则上使用数字作为键可以正常工作,但如果数字类型与[int]
不同,您可能需要使用 显式转换 才能访问此类条目:
$ht = @{ 10L = 'ten' } # suffix 'L' makes the number a [long] (System.Int64)
$ht[10] # !! FAILS, because 10 is an [int], so the key is not found.
$ht[[long] 10] # OK - explicit cast matches the key's actual type
要使用字符串键,引用它们,例如, '65486855'
.
索引符号始终需要引用才能访问字符串键:
$ht['65486855']
相比之下,点符号只需要引用字符串键,如果它们 看起来像 数字(如果它们将被解析为数字作为未引用的标记) :
$ht = @{ '10' = 'ten'; 'a1' = 'a-one' } # string keys
$ht['10'] # quoting required
$ht.'10' # ditto, because 10 without quotes would be a *number*
$ht.a1 # quoting *optional*, because a1 is parsed as a *string*
如@mklement0 所述,您正在为每个键的值分配一个未分配的变量 ($null
)。您只需将 $model
替换为 $value
.
注意:您还在 Get_Item()
调用中引用了一个不存在的密钥。
解决方案
$modelList = @{}
$key = 48525748
$value = "Dell P2217"
$modelList[$key] = $value
$key = 65486855
$value = "Dell P2217"
$modelList[$key] = $value
$key = 65486856
$value = "Dell P2217"
$modelList[$key] = $value
$key = 51505066
$value = "HP 22-incher"
$modelList[$key] = $value
write-host WHYYYY: $modelList[51505066]
write-host WHYY: $modelList.Get_Item(51505066)
我在使用哈希表将产品代码映射到型号名称时遇到问题:
$modelList = @{}
$key = 48525748
$value = "Dell P2217"
$modelList[$key] = $model
$key = 65486855
$value = "Dell P2217"
$modelList[$key] = $model
$key = 65486856
$value = "Dell P2217"
$modelList[$key] = $model
$key = 51505066
$value = "HP 22-incher"
$modelList[$key] = $model
write-host WHYYYY: $modelList[51505066]
write-host WHYY: $modelList.Get_Item(51505056)
上面写出来的就是WHY
.
为什么它不能检索我刚刚添加的项目?抱歉,我知道这是非常基础的,但我一辈子都弄不明白。
看起来你只是想将 $value
,而不是 $model
分配给你的哈希表条目 ,这可以解释为什么你没有输出(一个未初始化的变量在 PowerShell 中默认隐式 $null
)。
检测此类问题的一种方法是,如果您尝试获取未初始化变量的值,让 PowerShell 报告 错误,方法是 设置 Set-StrictMode -Version 1
或更高。
但是,考虑在您的情况下使用哈希表 literal,这完全不需要变量:
$modelList = @{
48525748 = "Dell P2217"
65486855 = "Dell P2218"
65486856 = "Dell P2219"
51505066 = "HP 22-incher"
}
$modelList[51505066] # -> 'HP 22-incher'
在一行中定义它也是一种选择,在这种情况下,您必须用 ;
$modelList = @{ 48525748 = "Dell P2217"; 65486855 = "Dell P2217"; 65486856 = "Dell P2217"; 51505066 = "HP 22-incher" }
就访问哈希表的条目而言,您有两种语法选择(除了调用参数化的.Item()
属性 / .get_Item()
方法):
索引符号:
$modelList[51505066]
点符号(与对象一样):
$modelList.51505066
但是,存在细微差别 - 见下文。
注意事项:
您正在使用 numbers 作为键,这里的具体类型是
[int]
(System.Int32
),PowerShell 根据值自动选择。虽然原则上使用数字作为键可以正常工作,但如果数字类型与
[int]
不同,您可能需要使用 显式转换 才能访问此类条目:$ht = @{ 10L = 'ten' } # suffix 'L' makes the number a [long] (System.Int64) $ht[10] # !! FAILS, because 10 is an [int], so the key is not found. $ht[[long] 10] # OK - explicit cast matches the key's actual type
要使用字符串键,引用它们,例如,
'65486855'
.索引符号始终需要引用才能访问字符串键:
$ht['65486855']
相比之下,点符号只需要引用字符串键,如果它们 看起来像 数字(如果它们将被解析为数字作为未引用的标记) :
$ht = @{ '10' = 'ten'; 'a1' = 'a-one' } # string keys $ht['10'] # quoting required $ht.'10' # ditto, because 10 without quotes would be a *number* $ht.a1 # quoting *optional*, because a1 is parsed as a *string*
如@mklement0 所述,您正在为每个键的值分配一个未分配的变量 ($null
)。您只需将 $model
替换为 $value
.
注意:您还在 Get_Item()
调用中引用了一个不存在的密钥。
解决方案
$modelList = @{}
$key = 48525748
$value = "Dell P2217"
$modelList[$key] = $value
$key = 65486855
$value = "Dell P2217"
$modelList[$key] = $value
$key = 65486856
$value = "Dell P2217"
$modelList[$key] = $value
$key = 51505066
$value = "HP 22-incher"
$modelList[$key] = $value
write-host WHYYYY: $modelList[51505066]
write-host WHYY: $modelList.Get_Item(51505066)