简体   繁体   English

Powershell 基于一个对象创建一个新的对象数组,其中某些项目将被删除

[英]Powershell create a new object array based on one, with certain items to be removed

I am trying to create a new array based on some rules I have to follow.我正在尝试根据我必须遵循的一些规则创建一个新数组。 I am sure there is some easier way to do this but I cannot figure it out!我相信有一些更简单的方法可以做到这一点,但我无法弄清楚!

$files array contains the following: $files数组包含以下内容:

Name                   Amount
-----------            --------------
KL                     3
KS                     55
KS                     54
KS                     50
KM                     4
KD                     1
KW                     4
KD                     3
KV                     4
KD                     4
KL                     7
KS                     1

I want to create a new array in the same order as the above, but remove all but the latest KD & KL name.我想以与上述相同的顺序创建一个新数组,但删除除最新的KD & KL名称之外的所有内容。 Which would look like:看起来像:

    KS                     55
KS                     54
KS                     50
KM                     4
KW                     4
KV                     4
KD                     4
KL                     7
KS                     1

So far I have been trying to forloop to work out the amount of KD & KL's but I am getting confused with how to just add the ones later in the list.到目前为止,我一直在尝试使用 forloop 来计算 KD 和 KL 的数量,但我对如何在列表的后面添加这些感到困惑。

Use an ordered dictionary to keep track of the last seen object with a given name:使用有序字典来跟踪最后看到的具有给定名称的对象:

$latest = [ordered]@{}

$files |ForEach-Object {
  $latest[$_.Name] = $_
}

$arrayWithOnlyLatest = $latest.Values

$arrayWithOnlyLatest now contains only the last seen objects with each name $arrayWithOnlyLatest现在只包含每个名字最后看到的对象

I'm sure there is a much more clever way, but instead of getting the last KD/KL, you could just do the opposite.我敢肯定有更聪明的方法,但不是获得最后一个 KD/KL,你可以做相反的事情。 Reverse the array, only keep the first KD/KL, then reverse the array back.反转数组,只保留第一个KD/KL,然后反转数组。

[array]::Reverse($files)

$kdfound = $klfound = $false

$files = switch -Regex ($files)
{
    'KD' {if($kdfound -eq $false){$kdfound = $true; $_}}
    'KL' {if($klfound -eq $false){$klfound = $true; $_}}
    default {$_}
}

[array]::Reverse($files)

$files

Output输出

Name,Amount
KS,55
KS,54
KS,50
KM,4
KW,4
KV,4
KD,4
KL,7
KS,1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PowerShell 创建一个新的 object 并将值添加到数组 - PowerShell create a new object and add the values to an array How to combine items from one PowerShell Array and one Powershell Object and produce a 3rd PowerShell Object? - How to combine items from one PowerShell Array and one Powershell Object and produce a 3rd PowerShell Object? 在 PowerShell 中创建一个新对象 - Create a new object in PowerShell 根据 Powershell 中的输入数组删除某些 AppPool - Remove Certain AppPool based on input Array in Powershell Powershell:将所有项目/属性加载到新对象中 - Powershell: Loading all items/properties into a new object Powershell:当数组中只剩下一个 object 时,如何正确计算对象数组中的项目数 - Powershell: How to properly count number of items in array of objects when there is only one object left in array 如何使用PowerShell创建新的ConfigurationElement对象,或防止更改现有对象来修改其源代码? - How can I create a new ConfigurationElement object using PowerShell or prevent a change to an existing one modifying its source? Powershell - 如何根据文件名创建文件名数组? - Powershell - How to create array of filenames based on filename? powershell-在新的属性中合并数组属性 - powershell - combine array properties in new one PowerShell 当 Where-Object 返回一项时,数组列表分配和 Where-Object 失败。 适用于 2 个以上的项目 - PowerShell Array list assignment and Where-Object fails when Where-Object returns one item. Works with 2+ items
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM