【问题标题】:Create a list of paired string values in PowerShell在 PowerShell 中创建成对字符串值列表
【发布时间】:2017-10-24 13:13:22
【问题描述】:

对于我正在做的项目,我需要检查文件中的一行中是否存在一对字符串。

我曾尝试使用这样的哈希表:

$makes = 'Ferrari', 'Ford', 'VW', 'Peugeot', 'Subaru'
$models = 'Enzo', 'Focus', 'Golf', '206', 'Impreza'

$table = @{}

for ($i = 0; $i -lt $makes.Length; $i++)
{
    $table.Add($makes[$i], $models[$i])
}

在我尝试插入重复的 make 之前,这很有效。我很快发现哈希表不接受重复。

那么有没有办法在 PowerShell 中创建双重字符串列表?在 C# 中很容易做到,但我发现没有办法在 PowerShell 中实现。

【问题讨论】:

    标签: list powershell hashtable


    【解决方案1】:

    只需对您的代码和逻辑进行少量更改:

    $makes = 'Ferrari', 'Ford', 'VW', 'Peugeot', 'Subaru'
    $models = 'Enzo', 'Focus', 'Golf', '206', 'Impreza'
    
    for ($i = 0; $i -lt $makes.Length; $i++){
        [array]$cars += New-Object psobject -Property @{
            make  = $makes[$i]
            model = $models[$i]
    
        }
    }
    

    这使用自定义psobject,将其强制转换为数组以便允许+=

    【讨论】:

    • 谢谢。效果很好!我从来不知道你可以这样修改数组。
    【解决方案2】:

    这是一种避免数组串联的方法(类似于 Python 的“列表理解”语法):

    $makes = 'Ferrari', 'Ford', 'VW', 'Peugeot', 'Subaru'
    $models = 'Enzo', 'Focus', 'Golf', '206', 'Impreza'
    
    $cars = 0..$makes.Length | ForEach-Object {
      [PSCustomObject] @{
        Make  = $makes[$_]
        Model = $models[$_]
      }
    }
    

    【讨论】:

    • 从您发布的内容来看,“O.”的含义并不清楚。对于我作为 PowerShell 新手用户来说,它看起来像是某种速记语法糖。又名缩短了原本会很清楚的东西。对于像我这样的其他新手,is 是 Range Opperator,在这里解释:docs.microsoft.com/en-us/powershell/module/…
    【解决方案3】:

    您可以使用 PSCustomObject 并直接填充列表:

    $cars = @(
        [pscustomobject]@{make = "Ferrari"; model="Enzo"}
        [pscustomobject]@{make = "Ford"; model="Focus"}
        [pscustomobject]@{make = "Peugeot"; model="206"}
        [pscustomobject]@{make = "Subaru"; model="Imprezza"}
    )
    

    【讨论】:

    • 如果我删除“[pscustomobject]”部分会有什么不同?它在 PS5 下的工作原理显然相同。谢谢。
    【解决方案4】:

    注意:Bill Stewart's answer 最有效地回答了所提出的问题 - 创建值对的 列表不考虑重复项
    此答案侧重于基于哈希表的解决方案,该解决方案在单个条目中收集给定品牌的所有模型,并允许按品牌高效查找模型。


    您仍然可以使用哈希表 - 使用它方便的基于键的查找 - 如果您将与给定品牌关联的所有模型存储为 array每个品牌 - 根据定义是唯一的 - 条目

    # Note the duplicate 'VW' entry at the end.
    $makes = 'Ferrari', 'Ford', 'VW', 'Peugeot', 'Subaru', 'VW'
    # Corresponding models.
    $models = 'Enzo', 'Focus', 'Golf', '206', 'Impreza', 'Polo'
    
    $table = [ordered] @{}; $i=0  # [ordered] (PSv3+) preserves the order of the keys
    foreach($make in $makes) {
      # Add the model at hand to the make's array of models.
      # The entry is created on demand as an array, due to [array]
      # (which creates an [object[]] array),
      # and for additional models the array is appended to.
      # You could also use [string[]], specifically.
      [array] $table[$make] += $models[$i++]
    }
    
    # Output the resulting hashtable
    $table
    

    这会产生:

    Name                           Value                                                                                                                                               
    ----                           -----                                                                                                                                               
    Ferrari                        {Enzo}                                                                                                                                              
    Ford                           {Focus}                                                                                                                                             
    VW                             {Golf, Polo}                                                                                                                                        
    Peugeot                        {206}                                                                                                                                               
    Subaru                         {Impreza}                                                                                                                                           
    

    注意VW 的值有2 个条目({...} 表示一个值是一个数组)。

    稍后要获取给定品牌的模型,只需使用:

    $vwModels = $table['VW']
    

    检查给定的品牌/型号对是否已包含在表中:

    $table[$make] -contains $model
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-15
      • 2012-11-26
      • 2020-04-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多