对于我正在进行的项目,我需要检查并查看文件中的一行中是否存在一对字符串。
我试过使用这样的哈希表:
$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中实现它。
答案 0 :(得分:4)
对代码和逻辑进行最小的更改:
$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
投射到数组,以便允许+=
。
答案 1 :(得分:4)
这是一种避免数组连接的方法(类似于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[$_]
}
}
答案 2 :(得分:3)
您可以使用PSCustomObject
并直接填充列表:
$cars = @(
[pscustomobject]@{make = "Ferrari"; model="Enzo"}
[pscustomobject]@{make = "Ford"; model="Focus"}
[pscustomobject]@{make = "Peugeot"; model="206"}
[pscustomobject]@{make = "Subaru"; model="Imprezza"}
)
答案 3 :(得分:2)
如果将与给定品牌相关联的所有型号存储为数组,您仍然可以使用哈希表 - 使用方便的基于密钥的查找每个制作 - 根据定义一个也是唯一的 - 条目:
# Note the duplicate 'VW' entry at the end.
$makes = 'Ferrari', 'Ford', 'VW', 'Peugeot', 'Subaru', 'VW'
$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.
# The entry is created on demand as an array, due to [object[]]
# ([array] works too, but it is [object[]] that is actually created),
# and for additional models the array is appended to.
# You could also use [string[]], specifically.
[object[]] $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
请注意,使用+=
扩展数组很方便,但很慢,因为实际上每次都必须创建 new 数组。
如果效果重要,请改为使用[System.Collections.ArrayList]
或通用[System.Collections.Generic.List[<type>]]
实例:
# Note the duplicate 'VW' entry at the end.
$makes = 'Ferrari', 'Ford', 'VW', 'Peugeot', 'Subaru', 'VW'
$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 list.
# Note the need to assign an *array* (when the entry is created),
# using the prepended array-construction operator, ",", which is preferable
# to @(...) for performance reasons.
# [Collections.Generic.List[string]] would work too.
[Collections.ArrayList] $table[$make] += , $models[$i++]
}
# Output the resulting hashtable
$table
请注意,PowerShell如何仅允许您使用[Collections.ArrayList]
而不是完整类型名称[System.Collections.ArrayList]
,因为隐含了第一个组件System
。