[英]Combine Powershell arrays
I have an array, call it $x in powershell. 我有一个数组,在powershell中将其称为$ x。
$x | ft -autosize
d c b
- - -
Apple true
Apple true
Banana
Banana true
Clark true
David true
David true
Eddy
Eddy
I'm trying to combine items. 我正在尝试合并项目。 The result I want in this case is:
在这种情况下,我想要的结果是:
$x | ft -autosize
d c b
- - -
Apple true true
Banana true
Clark true
David true true
Eddy
Note: the blanks can represent false, but the array is typically blank when false, and TRUE when true. 注意:空格可以表示false,但是如果为false,则数组通常为空白;如果为true,则数组为TRUE。 Lines are not always doubled up (as in Clark above)
行并不总是加倍(如上面的Clark所示)
I can loop thru the array, and collecting each element, 1 by 1, when I find a match (if any), but just keep thinking there has to be a better way. 我可以遍历数组,并在找到匹配项(如果有)的情况下以1到1的方式收集每个元素,但是一直想着必须有更好的方法。
The following should clean up your array in PowerShell by leveraging Group-Object
以下应该利用
Group-Object
清理PowerShell中的数组
Create an array called $exampleArray
: 创建一个名为
$exampleArray
的数组:
$exampleArray = @()
$names = @("Apple", "Banana", "Clark", "David", "Eddy")
$tOrB = @("True","")
#just for an example
1..20 | % {
$object = New-Object PSObject -Property @{
#Get random Name
d = $names[$(Get-Random -Maximum $names.Count)]
#do true / blank -- random
c = $tOrB[$(Get-Random -Maximum $tOrB.Count)]
b = $tOrB[$(Get-Random -Maximum $tOrB.Count)]
}
$exampleArray += $object
}
$exampleArray
We can then group by d
: 然后我们可以按
d
分组:
#group "combine" by column d
$group = $exampleArray | Group-Object d
Once grouped we can iterate through the grouped names and build a new array called $combined
: 分组后,我们可以遍历分组的名称并构建一个名为
$combined
的新数组:
$combined = @()
$group | %{
$object = New-Object PSObject -Property @{
d = $_.Name
}
if($_.Group.c -contains "True")
{
$object | Add-Member -MemberType NoteProperty -Name c -Value "True"
}
else
{
$object | Add-Member -MemberType NoteProperty -Name c -Value ""
}
if($_.Group.b -contains "True")
{
$object | Add-Member -MemberType NoteProperty -Name b -Value "True"
}
else
{
$object | Add-Member -MemberType NoteProperty -Name b -Value ""
}
$combined += $object
}
$combined
Note: If you already have the array loaded (in this case $exampleArray
), you might be better off just doing a Where-Object
off of that array... This is dependent on what exactly you are attempting to accomplish. 注意:如果已经加载了数组(在本例中
$exampleArray
),则最好从该数组上执行Where-Object
……这取决于您要尝试完成的工作。 The answer I provided is most likely over-kill 我提供的答案很可能是过度杀伤
Group-Object
is indeed the right cmdlet to use: Group-Object
确实是使用的正确cmdlet:
# Sample input (constructed from CSV data for convenience)
$x = @'
d,c,b
Apple,,true
Apple,true,
Banana,,
Banana,true,
Clark,,true,
David,,true
David,true,
Eddy,,
Eddy,,
'@ | ConvertFrom-Csv
$x | Group-Object d | # Group the array of objects by their .d property
ForEach-Object { # Process each resulting group
# For each .d value, construct a custom object that aggregates
# the .c and .b property values and output it.
[pscustomobject] @{
d = $_.Name # The shared .d value
c = $_.Group.c | Where-Object { $_ } # any non-empty .c values
b = $_.Group.b | Where-Object { $_ } # any non-empty .b values
}
}
The above yields: 以上收益:
d c b
- - -
Apple true true
Banana true
Clark true
David true true
Eddy
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.