Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Filter niggle

Reply
 
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
Old 11-12-2006   #2 (permalink)
Sung M Kim


 
 

Re: Filter niggle

You are basically returning everything in your filter
So make sure to return only even numbers using "if"

filter OnlyEven { if ($_ % 2 -eq 0) { $_ } }

[^_^]PS[131]+>1,2,3,4,5,6 | OnlyEven
2
4
6

My System SpecsSystem Spec
Old 11-12-2006   #3 (permalink)
dreeschkind


 
 

RE: Filter niggle

"Andrew Watt [MVP]" wrote:

> 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


The filter can't work inside a Where-Block like this, because the filter
always expects some input. Try this instead:

PS> 1,2,3,4,5,6 | where-object {$_|OnlyEven}
2
4
6

or if you like:

PS> 1,2,3,4,5,6 | where-object {$input|OnlyEven}
2
4
6

--
greetings
dreeschkind
My System SpecsSystem Spec
Old 11-12-2006   #4 (permalink)
Jacques Barathon [MS]


 
 

Re: Filter niggle

If you want to use it with where-object, define OnlyEven as a function, not
a filter:

PS> function onlyEven {
>> $_ %2 -eq 0
>> }
>>

PS> 1..6 | where {onlyEven}
2
4
6

Jacques

"Andrew Watt [MVP]" <SVGDeveloper@aol.com> wrote in message
news:i5uel295kt0lbsoas2bc3d5iq1r44up4rk@4ax.com...
>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
Old 11-12-2006   #5 (permalink)
Andrew Watt [MVP]


 
 

Re: Filter niggle

Thanks.

Could you have someone fix the relevant part of About_Filter.help.txt?

Thanks

Andrew Watt MVP

On Sun, 12 Nov 2006 21:47:25 +0100, "Jacques Barathon [MS]"
<jbaratho@online.microsoft.com> wrote:

>If you want to use it with where-object, define OnlyEven as a function, not
>a filter:
>
>PS> function onlyEven {
>>> $_ %2 -eq 0
>>> }
>>>

>PS> 1..6 | where {onlyEven}
>2
>4
>6
>
>Jacques
>
>"Andrew Watt [MVP]" <SVGDeveloper@aol.com> wrote in message
>news:i5uel295kt0lbsoas2bc3d5iq1r44up4rk@4ax.com...
>>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
Old 11-12-2006   #6 (permalink)
Andrew Watt [MVP]


 
 

Re: Filter niggle

On Sun, 12 Nov 2006 12:32:01 -0800, dreeschkind
<dreeschkind@discussions.microsoft.com> wrote:

>PS> 1,2,3,4,5,6 | where-object {$_|OnlyEven}


Thanks. That works.

Andrew Watt MVP
My System SpecsSystem Spec
Old 11-16-2006   #7 (permalink)
Jacques Barathon [MS]


 
 

Re: Filter niggle

"Andrew Watt [MVP]" <SVGDeveloper@aol.com> wrote in message
news:gc2fl251278ql4fqbn08s0210k32oh62tt@4ax.com...
> Thanks.
>
> Could you have someone fix the relevant part of About_Filter.help.txt?
>
> Thanks
>
> Andrew Watt MVP


Please raise it on Connect.

Jacques

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Feeds filter Live Mail
Junk Filter doesn't filter Live Mail
Windows Mail - minor niggle Vista General
help. Digital filter? how to de-filter? USB audio device Vista hardware & devices
Dual boot niggle Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46