Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

meaning of -eq $null

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 11-15-2007   #1 (permalink)
Leo Tohill
Guest


 

meaning of -eq $null

a comparison of an empty list to $null doesn't return anything. Not true,
not false, just nothing. Example:

$t1 = new-object System.Collections.ArrayList
$t1 -eq $null
(returns nothing)
$t1 -ne $null)
(returns nothing)

I'm familiar with three-valued logic in database systems but I suspect that
something else is going on here. Could someone explain?



My System SpecsSystem Spec
Old 11-15-2007   #2 (permalink)
Karl Prosser[MVP]
Guest


 

Re: meaning of -eq $null

Ok this is one of the serious gotchas in powershell.. which is serious
because you'll often make this mistake, and its not intuitive coming
from other languages but its actually a powerful feature when used right..

basically with the comparison operation.. its based on the type of the
left operand. If its a single object then the comparison is going to
return true, or false.. but if the left operand is a collection or
array, its going to do pattern matching, and then just spit out the
matches. that is why when i am doing explicit compares i always do what
looks unnatural, and put the single item on the left i.e

if (2 -eq $a)

instead of what you'd naturally put

if ($a -eq 2 )

now lets go a little deeper with this.

@() -eq $null
Quote:
Quote:

>>nothing is returned
$null -eq @()
Quote:
Quote:

>>false, an empty array is not a null object.
$null -eq @($null)
Quote:
Quote:

>>false and $null is not the same as an array with a null in it.
anyway Null is a comfusing example.. but lets look at your one..

if $t1 is undefined then

$t1 -eq $null
and
$null -eq $t1

both show true and an undefined variable will resolve to a single true..
but hte minute you turn it into a collection

$t1 = new-object System.Collections.ArrayList
$t1 -eq $null

you get NOTHING... because you are not taking each item in the arraylist
(which there are none) and doing pattern matching and just returning the
ones that match the comparision..

however for your test

$null -eq $t1
returns false, because t1 does indeed exit.

if lets add some items to $t1

$t1.Add(2)
$t1.Add(5)
$t1.Add($null)
$t1.Add(2)

and now
$t1 -eq 2
returns 2 lots of 2.... because its matching the items in the array

which
2 -eq $t1 still returns false, since an arraylist is not an integer let
alone a 2

interestingly enough

$t1 -eq $null still returns nothing.. this here i don't
understand/remember at the moment.. maybe its stripping it out somewhere

but you do see the pattern matching behaviour if the left is an array or
collection and why if you want to be sure you are doing a true single
object comparison, put the singleobject on the left..

this pattern matching if the left is a collection works for all/most of
the comparison operators

1..10 -gt 5
Quote:
Quote:

>>returns 6 through 10
"karl","matt","john","jim" -match "a"

etc

well have to look into the issues of that one $null in the collection
not coming through, but i hope you get the general picture now..

Sincerley,
Karl

One of the Architects of Powershell Analyzer and PowerShell Plus
http://www.powershell.com






Leo Tohill wrote:
Quote:

> a comparison of an empty list to $null doesn't return anything. Not true,
> not false, just nothing. Example:
>
> $t1 = new-object System.Collections.ArrayList
> $t1 -eq $null
> (returns nothing)
> $t1 -ne $null)
> (returns nothing)
>
> I'm familiar with three-valued logic in database systems but I suspect that
> something else is going on here. Could someone explain?
>
>
My System SpecsSystem Spec
Old 11-16-2007   #3 (permalink)
Leo Tohill
Guest


 

Re: meaning of -eq $null

As usual, an excellent response. Thanks Karl.

I'd phrase the important point as "comparison operators are not left-right
symmetrical". Once you accept that, things become clearer.

Let me know when you figure out that remaining question on "$t1 -eq $null" .
There must be another rule in effect.

I don't suppose the language specification is public, is it?

- Leo

"Karl Prosser[MVP]" wrote:
Quote:

> Ok this is one of the serious gotchas in powershell.. which is serious
> because you'll often make this mistake, and its not intuitive coming
> from other languages but its actually a powerful feature when used right..
>
> basically with the comparison operation.. its based on the type of the
> left operand. If its a single object then the comparison is going to
> return true, or false.. but if the left operand is a collection or
> array, its going to do pattern matching, and then just spit out the
> matches. that is why when i am doing explicit compares i always do what
> looks unnatural, and put the single item on the left i.e
>
> if (2 -eq $a)
>
> instead of what you'd naturally put
>
> if ($a -eq 2 )
>
> now lets go a little deeper with this.
>
> @() -eq $null
Quote:
Quote:

> >>nothing is returned
>
> $null -eq @()
Quote:
Quote:

> >>false, an empty array is not a null object.
>
> $null -eq @($null)
Quote:
Quote:

> >>false and $null is not the same as an array with a null in it.
>
> anyway Null is a comfusing example.. but lets look at your one..
>
> if $t1 is undefined then
>
> $t1 -eq $null
> and
> $null -eq $t1
>
> both show true and an undefined variable will resolve to a single true..
> but hte minute you turn it into a collection
>
> $t1 = new-object System.Collections.ArrayList
> $t1 -eq $null
>
> you get NOTHING... because you are not taking each item in the arraylist
> (which there are none) and doing pattern matching and just returning the
> ones that match the comparision..
>
> however for your test
>
> $null -eq $t1
> returns false, because t1 does indeed exit.
>
> if lets add some items to $t1
>
> $t1.Add(2)
> $t1.Add(5)
> $t1.Add($null)
> $t1.Add(2)
>
> and now
> $t1 -eq 2
> returns 2 lots of 2.... because its matching the items in the array
>
> which
> 2 -eq $t1 still returns false, since an arraylist is not an integer let
> alone a 2
>
> interestingly enough
>
> $t1 -eq $null still returns nothing.. this here i don't
> understand/remember at the moment.. maybe its stripping it out somewhere
>
> but you do see the pattern matching behaviour if the left is an array or
> collection and why if you want to be sure you are doing a true single
> object comparison, put the singleobject on the left..
>
> this pattern matching if the left is a collection works for all/most of
> the comparison operators
>
> 1..10 -gt 5
Quote:
Quote:

> >>returns 6 through 10
>
> "karl","matt","john","jim" -match "a"
>
> etc
>
> well have to look into the issues of that one $null in the collection
> not coming through, but i hope you get the general picture now..
>
> Sincerley,
> Karl
>
> One of the Architects of Powershell Analyzer and PowerShell Plus
> http://www.powershell.com
>
>
>
>
>
>
> Leo Tohill wrote:
Quote:

> > a comparison of an empty list to $null doesn't return anything. Not true,
> > not false, just nothing. Example:
> >
> > $t1 = new-object System.Collections.ArrayList
> > $t1 -eq $null
> > (returns nothing)
> > $t1 -ne $null)
> > (returns nothing)
> >
> > I'm familiar with three-valued logic in database systems but I suspect that
> > something else is going on here. Could someone explain?
> >
> >
>
My System SpecsSystem Spec
Old 11-16-2007   #4 (permalink)
Leo Tohill
Guest


 

Re: meaning of -eq $null

Some more thoughts on this...

$t1 = new-object System.Collections.ArrayList
$t1 -eq $null

returns nothing, because a comparison is never performed! The comparison is
not performed, because the left side is an empty collection.




My System SpecsSystem Spec
Old 11-16-2007   #5 (permalink)
Leo Tohill
Guest


 

Re: meaning of -eq $null

BTW, I'm just restating what you implied. I just wanted to point out that
the
"comparison is never performed" realization opened up my Powershell mind
just a bit.


"Leo Tohill" wrote:
Quote:

> Some more thoughts on this...
>
> $t1 = new-object System.Collections.ArrayList
> $t1 -eq $null
>
> returns nothing, because a comparison is never performed! The comparison is
> not performed, because the left side is an empty collection.
>
>
>
>
My System SpecsSystem Spec
Old 11-16-2007   #6 (permalink)
Karl Prosser[MVP]
Guest


 

Re: meaning of -eq $null

i like the way you worded the observations..

what i find interesting though is with $null things are still different i.e

1,4,"hello",$null,1,$null -eq 1

returns 1 ones, from the collection/array

but

1,4,"hello",$null,1,$null -eq $null

doesn't return anything even though the array contains nulls.. to me
this is strange. a side effect i discovered when answering your questions.
My System SpecsSystem Spec
Old 11-16-2007   #7 (permalink)
Keith Hill [MVP]
Guest


 

Re: meaning of -eq $null

Nice explanation!
Quote:

> $t1 -eq $null still returns nothing.. this here i don't
> understand/remember at the moment.. maybe its stripping it out somewhere
It does return $null but $null doesn't render to any text on the console
e.g.

15> $t1 -eq $null
16> $t1 -eq $null | %{if($_ -eq $null){"We have a null"}}
We have a null

--
Keith


"Karl Prosser[MVP]" <karl@xxxxxx_o_w_e_r_s_h_e_l_l.com> wrote in message
news:eO1GIrAKIHA.536@xxxxxx
Quote:

> Ok this is one of the serious gotchas in powershell.. which is serious
> because you'll often make this mistake, and its not intuitive coming from
> other languages but its actually a powerful feature when used right..
>
> basically with the comparison operation.. its based on the type of the
> left operand. If its a single object then the comparison is going to
> return true, or false.. but if the left operand is a collection or array,
> its going to do pattern matching, and then just spit out the matches. that
> is why when i am doing explicit compares i always do what looks unnatural,
> and put the single item on the left i.e
>
> if (2 -eq $a)
>
> instead of what you'd naturally put
>
> if ($a -eq 2 )
>
> now lets go a little deeper with this.
>
> @() -eq $null
Quote:
Quote:

> >>nothing is returned
>
> $null -eq @()
Quote:
Quote:

> >>false, an empty array is not a null object.
>
> $null -eq @($null)
Quote:
Quote:

> >>false and $null is not the same as an array with a null in it.
>
> anyway Null is a comfusing example.. but lets look at your one..
>
> if $t1 is undefined then
>
> $t1 -eq $null
> and
> $null -eq $t1
>
> both show true and an undefined variable will resolve to a single true..
> but hte minute you turn it into a collection
>
> $t1 = new-object System.Collections.ArrayList
> $t1 -eq $null
>
> you get NOTHING... because you are not taking each item in the arraylist
> (which there are none) and doing pattern matching and just returning the
> ones that match the comparision..
>
> however for your test
>
> $null -eq $t1
> returns false, because t1 does indeed exit.
>
> if lets add some items to $t1
>
> $t1.Add(2)
> $t1.Add(5)
> $t1.Add($null)
> $t1.Add(2)
>
> and now
> $t1 -eq 2
> returns 2 lots of 2.... because its matching the items in the array
>
> which
> 2 -eq $t1 still returns false, since an arraylist is not an integer let
> alone a 2
>
> interestingly enough
>
> $t1 -eq $null still returns nothing.. this here i don't
> understand/remember at the moment.. maybe its stripping it out somewhere
>
> but you do see the pattern matching behaviour if the left is an array or
> collection and why if you want to be sure you are doing a true single
> object comparison, put the singleobject on the left..
>
> this pattern matching if the left is a collection works for all/most of
> the comparison operators
>
> 1..10 -gt 5
Quote:
Quote:

> >>returns 6 through 10
>
> "karl","matt","john","jim" -match "a"
>
> etc
>
> well have to look into the issues of that one $null in the collection not
> coming through, but i hope you get the general picture now..
My System SpecsSystem Spec
Old 11-16-2007   #8 (permalink)
Karl Prosser[MVP]
Guest


 

Re: meaning of -eq $null

thanks keith you are right

(1,4,"hello",$null,1,$null -eq $null).length

shows
two

i should have just gone to the results explorer view in powershell
analyzer because its as clear as day there, but i was looking at the
propertygrid, which i exclude nulls f