gpt4 book ai didi

arrays - 在 PSCustomObject 数组中查询具有最大值的行

转载 作者:行者123 更新时间:2023-12-02 22:57:22 25 4
gpt4 key购买 nike

我试图找到一个属性大于另一行属性的行。示例:

$Array
Name Value
---- ----
test1 105
test2 101
test3 512 <--- Selects this row as it is the largest value

这是我尝试“1 行”的尝试,但它不起作用。

$Array | % { If($_.value -gt $Array[0..($Array.Count)].value){write-host "$_.name is the largest row"}}

目前它没有任何输出。

期望的输出:

"test1 is the largest row"

我无法想象如何在没有一些严肃的意大利面条代码的情况下有效地做到这一点。

最佳答案

您可以利用 Sort-Object 像这样按属性“值”对它们进行排名

$array = @(
[PSCustomObject]@{Name='test1';Value=105}
[PSCustomObject]@{Name='test2';Value=101}
[PSCustomObject]@{Name='test3';Value=512}
)

$array | Sort-Object -Property value -Descending | Select-Object -First 1

输出

Name  Value
---- -----
test3 512

要合并您的写入主机,您只需通过 foreach 运行您选择的主机即可。

$array | Sort-Object -Property value -Descending |
Select-Object -First 1 | Foreach-Object {Write-host $_.name,"has the highest value"}

test3 has the highest value

或者捕获到一个变量

$Largest = $array | Sort-Object -Property value -Descending | Select-Object -First 1
Write-host $Largest.name,"has the highest value"

test3 has the highest value

关于arrays - 在 PSCustomObject 数组中查询具有最大值的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64344832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com