$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?
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
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
$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.
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.
> $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...
(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
----------------------------------------------------------
DotNetNuke Corp. ASP.Net MVP
www.dotnetnuke.com
----------------------------------------------------------