PowerShell查询哈希表的嵌套子元素
在 PowerShell 中,我想统计所有使用球的运动。挑战在于它们的级别低于我可以通过直接Where-Object 查询访问的级别。这是哈希表:
$league = @{
school = @{
name = 'Lincoln High School'
levels = ('9','10','11','12')
}
sports = @{
football = @{
coed = 'b'
season = 'fall'
balls = $true
}
hockey = @{
coed = 'b'
season = 'winter'
balls = $false
}
lacrosse = @{
coed = 'g'
season = 'spring'
balls = $true
}
swim = @{
coed = 'c'
season = 'winter'
balls = $false
}
}
}
如何设计查询以跳到balls
?每项运动在关键级别的命名都不同,并且通配符似乎不起作用。
( $league.sports | Where-Object { $_.[?].balls -eq $true } ).count
我需要一些东西来连接这项运动的儿童元素。
In PowerShell, I want to get a count of all sports that use balls. The challenge is that they're a level lower than I can access with a direct Where-Object query. Here's the hashtable:
$league = @{
school = @{
name = 'Lincoln High School'
levels = ('9','10','11','12')
}
sports = @{
football = @{
coed = 'b'
season = 'fall'
balls = $true
}
hockey = @{
coed = 'b'
season = 'winter'
balls = $false
}
lacrosse = @{
coed = 'g'
season = 'spring'
balls = $true
}
swim = @{
coed = 'c'
season = 'winter'
balls = $false
}
}
}
How do I fashion the query to skip to the balls
? Each sport is named differently at the key level, and wildcards don't seem to work.
( $league.sports | Where-Object { $_.[?].balls -eq $true } ).count
I need something to bridge to the child element of the sport.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
.where(..)
:使用
Where-Object
:如您所见,这两种方法都需要使用
.GetEnumerator()
方法。另一种更详细的替代方案:
Using
.where(..)
:Using
Where-Object
:As you can see, both methods require the use of
.GetEnumerator()
method.Another more verbose alternative: