Thread: Filter niggle
View Single Post
Old 11-12-2006   #1 (permalink)
Andrew Watt [MVP]


 
 

Filter niggle

I must be doing something very basic wrong here but just can't see it.

I'm trying to create a simple filter to filter out odd numbered
values.

The code works when inside the script block of where-object.

PS Alias:\> 1,2,3,4,5,6 | where-object {$_ % 2 -eq 0}
2
4
6

I copied and pasted it to the Filter definition:

PS Alias:\> Filter OnlyEven {
>> $_ % 2 -eq 0
>> }
>>


And ran it as follows (when it didn't filter).

PS Alias:\> 1,2,3,4,5,6 | where-object {OnlyEven}
1
2
3
4
5
6

However, the Filter on its own does return True and False for the
appropriate values.

PS Alias:\> 1,2,3,4,5,6 | OnlyEven
False
True
False
True
False
True
PS Alias:\>

What very simple thing am I missing? Why aren't the objects discarded
by where-object when I use the Filter name inside the script block?

It's a niggle rather than a problem since I can just write any
necessary code in the script block and bypass filters altogether.

FWIW the example in about_filter.help.txt doesn't work for me either.

Waiting for someone to explain the "Doh!" moment to me.

Thanks

Andrew Watt MVP
My System SpecsSystem Spec