在PowerShell中创建一组成对的字符串数值列表。

8

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

我尝试使用哈希表来解决这个问题:

$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])
}

这很好用,但是当我尝试插入一个重复的制造商时它就出现问题了。我很快发现散列表不接受重复项。
那么在PowerShell中有没有创建字符串双列表的方法?这在C#中非常容易,但我发现在PowerShell中无法实现。
4个回答

10
您可以使用PSCustomObject,并直接填充列表:
$cars = @(
    [pscustomobject]@{make = "Ferrari"; model="Enzo"}
    [pscustomobject]@{make = "Ford"; model="Focus"}
    [pscustomobject]@{make = "Peugeot"; model="206"}
    [pscustomobject]@{make = "Subaru"; model="Imprezza"}
)

2
如果我删除“[pscustomobject]”部分,这会有什么不同?在PS5下似乎工作方式相同。谢谢。 - Raúl Salinas-Monteagudo

5

通过最小化代码和逻辑的更改:

$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,转换成一个数组,以便可以使用+=


谢谢。运行得很好!我从来不知道你可以像那样修改数组。 - AJennings1

4
这里有一种避免数组连接的方法(类似于 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用户来说,它看起来像是某种速记语法糖。即缩短本来应该很清晰的东西。对于像我这样的其他新手,它是范围运算符,并在此处解释:https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-5.1#range-operator- - generic-user

2
注意:所提问的问题——创建一个值对列表,无论是否存在重复项——最有效的答案是Bill Stewart的答案。本答案侧重于一种基于哈希表的解决方案,它将给定制造商的所有型号收集到单个条目中,并允许通过制造商高效查找型号。
如果您将每个制造商的所有型号作为一个数组存储在该制造商的唯一的入口中,那么您仍然可以使用哈希表进行方便、基于关键字的查找:

# 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接