我正在尝试在管道中使用if
。
我知道有where
(别名为?
)过滤器,但如果我只想在满足某些条件时激活过滤器怎么办?
我的意思是,例如:
get-something | ? {$_.someone -eq 'somespecific'} | format-table
如何在管道中使用if
来打开/关闭过滤器?这可行吗?有意义吗?
谢谢
编辑以澄清
如果没有管道,则会像这样:
if($filter) { get-something | ? {$_.someone -eq 'somespecific'} } else { get-something }
回答riknik后的编辑
愚蠢的示例,展示了我所寻找的内容。您有一个保存在变量$data
中的非范式化数据表,并且您想要执行一种“钻取”数据过滤:
function datafilter { param([switch]$ancestor, [switch]$parent, [switch]$child, [string]$myancestor, [string]$myparent, [string]$mychild, [array]$data=[]) $data | ? { (!$ancestor) -or ($_.ancestor -match $myancestor) } | ? { (!$parent) -or ($_.parent -match $myparent) } | ? { (!$child) -or ($_.child -match $mychild) } |
}
例如,如果我只想按特定的父级进行过滤:
datafilter -parent -myparent 'myparent' -data $mydata
这是一种非常优雅、性能良好且简单的利用?
的方式。试着使用if
做同样的事情,你会明白我的意思。