![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
| | #6 (permalink) |
| | 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 Specs![]() |
| | #7 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||