Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

meaning of -eq $null

11 views
Skip to first unread message

Leo Tohill

unread,
Nov 15, 2007, 10:33:01 PM11/15/07
to
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?


Karl Prosser[MVP]

unread,
Nov 15, 2007, 11:37:01 PM11/15/07
to
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
>>nothing is returned

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

$null -eq @($null)
>>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
>>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

unread,
Nov 16, 2007, 8:32:03 AM11/16/07
to
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

Leo Tohill

unread,
Nov 16, 2007, 10:41:01 AM11/16/07
to
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.


Leo Tohill

unread,
Nov 16, 2007, 11:35:02 AM11/16/07
to
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.

Karl Prosser[MVP]

unread,
Nov 16, 2007, 1:18:24 PM11/16/07
to
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.

Keith Hill [MVP]

unread,
Nov 16, 2007, 1:22:22 PM11/16/07
to
Nice explanation!

> $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@p_o_w_e_r_s_h_e_l_l.com> wrote in message
news:eO1GIrA...@TK2MSFTNGP06.phx.gbl...

Karl Prosser[MVP]

unread,
Nov 16, 2007, 1:34:30 PM11/16/07
to
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 from, for good reason.

Joe Brinkman

unread,
Nov 16, 2007, 7:53:56 PM11/16/07
to
Don't you just love it when you have so many features in your own software
that you forget that they exist ;-)

Joe Brinkman
----------------------------------------------------------
DotNetNuke Corp. ASP.Net MVP
www.dotnetnuke.com
----------------------------------------------------------

0 new messages