在 PowerShell 中创建成对的字符串值列表

Create a list of paired string values in PowerShell

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

我试过像这样使用散列 table:

$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 之前,此方法运行良好。我很快发现哈希 tables 不接受重复项。

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

对您的代码和逻辑进行最少的更改:

$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,强制转换为数组,以便允许 +=

这是一种避免数组连接的方法(类似于Python "list comprehension"语法):

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

$cars = 0..$makes.Length | ForEach-Object {
  [PSCustomObject] @{
    Make  = $makes[$_]
    Model = $models[$_]
  }
}

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

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

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


您仍然可以使用散列table - 具有方便的基于键的查找 - 如果您存储与给定关联的所有模型make 作为每个 make 中的 array - 根据定义,只有一个 - entry:

# 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:

$table[$make] -contains $model