【问题标题】:Powershell Hashtable Retrieval TroublePowershell Hashtable 检索问题
【发布时间】:2018-09-12 20:04:51
【问题描述】:

我在使用哈希表将产品代码映射到型号名称时遇到问题:

$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
为什么它无法检索我刚刚添加的项目?抱歉,我知道这是非常基本的,但我终其一生都无法弄清楚。

【问题讨论】:

  • $model 为空?

标签: powershell hashtable


【解决方案1】:

看起来您只是想将$value,而不是$model 分配给您的哈希表条目,这可以解释为什么您没有得到输出(默认情况下,未初始化的变量隐含为$null在 PowerShell 中)。
检测此类问题的一种方法是通过设置Set-StrictMode -Version 1,如果您尝试获取未初始化变量的值,则使 PowerShell 报告错误 > 或更高。


但是,考虑在您的情况下使用散列表文字,这样就完全不需要变量了:

$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 会根据值自动选择。 p>

    • 虽然原则上使用数字作为键可以正常工作,但如果数字类型[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*
      

【讨论】:

  • 我觉得这应该是默认的 pssa 规则(使用非字符串类型作为哈希表中的键)。顺便说一句,我不知道您可以像其他语言(10L)一样在 powershell 中为数字添加后缀来更改显式类型
  • @TheIncorrigible1:使用非字符串键绝对应该是经过深思熟虑的选择。除了用于[long]L,还有用于[decimal]D(以及二进制乘数后缀kbmb,...)。听起来还有几个后缀即将推出:请参阅 github.com/PowerShell/PowerShell/pull/7575github.com/PowerShell/PowerShell/issues/7557
【解决方案2】:

正如@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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-19
    • 2013-10-30
    • 1970-01-01
    • 2011-10-14
    • 2016-03-28
    • 2015-07-26
    • 1970-01-01
    相关资源
    最近更新 更多