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 - Compare-object doesn't seem to work with Arrays

Reply
 
Old 05-30-2007   #1 (permalink)
Chris Harris


 
 

Compare-object doesn't seem to work with Arrays

I think I'm doing this right... Any ideas?

PS C:\Toolbox\Scripts\DNSCheck> $t
TLD5.ULTRADNS.INFO
TLD6.ULTRADNS.CO.UK
TLD1.ULTRADNS.NET
TLD2.ULTRADNS.NET
TLD3.ULTRADNS.org
TLD4.ULTRADNS.org
PS C:\Toolbox\Scripts\DNSCheck> $v
TLD4.ULTRADNS.org
TLD5.ULTRADNS.INFO
TLD6.ULTRADNS.CO.UK
TLD1.ULTRADNS.NET
TLD2.ULTRADNS.NET
TLD3.ULTRADNS.org
PS C:\Toolbox\Scripts\DNSCheck> compare-object -ref $t -diff $v
Compare-Object : Cannot bind argument to parameter 'ReferenceObject' because
it is null.
At line:1 char:20
+ compare-object -ref <<<< $t -diff $v
PS C:\Toolbox\Scripts\DNSCheck>

My System SpecsSystem Spec
Old 05-30-2007   #2 (permalink)
Chris Harris


 
 

RE: Compare-object doesn't seem to work with Arrays

Actually - after doing some testing it looks like there is a problem with the
way I created my arrays (they don't appear to be arrays).

Here is the behavior I expected to see:

PS C:\Toolbox\Scripts\DNSCheck> $a = @(1,2,3,4)
PS C:\Toolbox\Scripts\DNSCheck> $b = @(1,4)
PS C:\Toolbox\Scripts\DNSCheck> compare-object $a $b

InputObject SideIndicator
----------- -------------
2 <=
3 <=

Now I just need to figure out what's wrong with my arrays...

-Chris

"Chris Harris" wrote:

> I think I'm doing this right... Any ideas?
>
> PS C:\Toolbox\Scripts\DNSCheck> $t
> TLD5.ULTRADNS.INFO
> TLD6.ULTRADNS.CO.UK
> TLD1.ULTRADNS.NET
> TLD2.ULTRADNS.NET
> TLD3.ULTRADNS.org
> TLD4.ULTRADNS.org
> PS C:\Toolbox\Scripts\DNSCheck> $v
> TLD4.ULTRADNS.org
> TLD5.ULTRADNS.INFO
> TLD6.ULTRADNS.CO.UK
> TLD1.ULTRADNS.NET
> TLD2.ULTRADNS.NET
> TLD3.ULTRADNS.org
> PS C:\Toolbox\Scripts\DNSCheck> compare-object -ref $t -diff $v
> Compare-Object : Cannot bind argument to parameter 'ReferenceObject' because
> it is null.
> At line:1 char:20
> + compare-object -ref <<<< $t -diff $v
> PS C:\Toolbox\Scripts\DNSCheck>

My System SpecsSystem Spec
Old 05-30-2007   #3 (permalink)
Marco Shaw


 
 

Re: Compare-object doesn't seem to work with Arrays

Chris Harris wrote:
> Actually - after doing some testing it looks like there is a problem with the
> way I created my arrays (they don't appear to be arrays).
>
> Here is the behavior I expected to see:
>
> PS C:\Toolbox\Scripts\DNSCheck> $a = @(1,2,3,4)
> PS C:\Toolbox\Scripts\DNSCheck> $b = @(1,4)
> PS C:\Toolbox\Scripts\DNSCheck> compare-object $a $b
>
> InputObject SideIndicator
> ----------- -------------
> 2 <=
> 3 <=
>
> Now I just need to figure out what's wrong with my arrays...


Using the gettype method will allow you to see what kind of object it is.

15# $a=5
16# $a.gettype()

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int32 System.ValueType


17#

My System SpecsSystem Spec
Old 05-30-2007   #4 (permalink)
Jacques Barathon [MS]


 
 

Re: Compare-object doesn't seem to work with Arrays

"Chris Harris" <ChrisHarris@discussions.microsoft.com> wrote in message
news:571754BB-E59B-489E-9584-A20C36D98CF2@microsoft.com...
> Actually - after doing some testing it looks like there is a problem with
> the
> way I created my arrays (they don't appear to be arrays).
>
> Here is the behavior I expected to see:
>
> PS C:\Toolbox\Scripts\DNSCheck> $a = @(1,2,3,4)
> PS C:\Toolbox\Scripts\DNSCheck> $b = @(1,4)
> PS C:\Toolbox\Scripts\DNSCheck> compare-object $a $b
>
> InputObject SideIndicator
> ----------- -------------
> 2 <=
> 3 <=
>
> Now I just need to figure out what's wrong with my arrays...


What you are actually trying to reproduce is this:

PS> $a=@(1,4)
PS> $b=@(4,1)
PS> compare-object $a $b
PS>

As you can see, compare-object does not capture differences such as similar
values sorted differently in an array. So if you want to identify sort
differences between $t and $v you will have to write your own comparison
routine. That shouldn't be too difficult though. A very basic example to get
you started:

PS> for ($i=0;$i -lt [System.Math]::Max($t.length,$v.length);$i++) {
>> if ($t[$i] -ne $v[$i]) {"$i : $($t[$i]) <> $($v[$i])"}
>> }
>>

0 : TLD5.ULTRADNS.INFO <> TLD4.ULTRADNS.org
1 : TLD6.ULTRADNS.CO.UK <> TLD5.ULTRADNS.INFO
2 : TLD1.ULTRADNS.NET <> TLD6.ULTRADNS.CO.UK
3 : TLD2.ULTRADNS.NET <> TLD1.ULTRADNS.NET
4 : TLD3.ULTRADNS.org <> TLD2.ULTRADNS.NET
5 : TLD4.ULTRADNS.org <> TLD3.ULTRADNS.org

Hope that helps,
Jacques

My System SpecsSystem Spec
Old 05-31-2007   #5 (permalink)
Chris Harris


 
 

Re: Compare-object doesn't seem to work with Arrays

Hi Jacques - thanks for the reply.

I'm not concerned about the sorting issues because I'm sorting the arrays
before comparing them. What I'm concerned about is one array having
more/less/different entries than the other.

Here I have a hashtable where the values are arrays.

Name Value
---- -----
ns2.microsage.net {bbns.blackbaud.com, bbns2.blackbaud.com}
ns1.microsage.net



On a side note, does anyone know what's going on in the example below?

PS C:\Toolbox\Scripts\DNSCheck> $a = @("ns1.microsage.net",
"ns2.microsage.net")
PS C:\Toolbox\Scripts\DNSCheck> $b = @("ns1.microsage.net")
PS C:\Toolbox\Scripts\DNSCheck> $a -eq $b
ns1.microsage.net
PS C:\Toolbox\Scripts\DNSCheck> $a -ne $b
ns2.microsage.net
PS C:\Toolbox\Scripts\DNSCheck>
PS C:\Toolbox\Scripts\DNSCheck> $b = $a
PS C:\Toolbox\Scripts\DNSCheck> $a
ns1.microsage.net
ns2.microsage.net
PS C:\Toolbox\Scripts\DNSCheck> $b
ns1.microsage.net
ns2.microsage.net
PS C:\Toolbox\Scripts\DNSCheck> $a -eq $b
PS C:\Toolbox\Scripts\DNSCheck> $a -ne $b
ns1.microsage.net
ns2.microsage.net


Thanks,
Chris


"Jacques Barathon [MS]" wrote:

> "Chris Harris" <ChrisHarris@discussions.microsoft.com> wrote in message
> news:571754BB-E59B-489E-9584-A20C36D98CF2@microsoft.com...
> > Actually - after doing some testing it looks like there is a problem with
> > the
> > way I created my arrays (they don't appear to be arrays).
> >
> > Here is the behavior I expected to see:
> >
> > PS C:\Toolbox\Scripts\DNSCheck> $a = @(1,2,3,4)
> > PS C:\Toolbox\Scripts\DNSCheck> $b = @(1,4)
> > PS C:\Toolbox\Scripts\DNSCheck> compare-object $a $b
> >
> > InputObject SideIndicator
> > ----------- -------------
> > 2 <=
> > 3 <=
> >
> > Now I just need to figure out what's wrong with my arrays...

>
> What you are actually trying to reproduce is this:
>
> PS> $a=@(1,4)
> PS> $b=@(4,1)
> PS> compare-object $a $b
> PS>
>
> As you can see, compare-object does not capture differences such as similar
> values sorted differently in an array. So if you want to identify sort
> differences between $t and $v you will have to write your own comparison
> routine. That shouldn't be too difficult though. A very basic example to get
> you started:
>
> PS> for ($i=0;$i -lt [System.Math]::Max($t.length,$v.length);$i++) {
> >> if ($t[$i] -ne $v[$i]) {"$i : $($t[$i]) <> $($v[$i])"}
> >> }
> >>

> 0 : TLD5.ULTRADNS.INFO <> TLD4.ULTRADNS.org
> 1 : TLD6.ULTRADNS.CO.UK <> TLD5.ULTRADNS.INFO
> 2 : TLD1.ULTRADNS.NET <> TLD6.ULTRADNS.CO.UK
> 3 : TLD2.ULTRADNS.NET <> TLD1.ULTRADNS.NET
> 4 : TLD3.ULTRADNS.org <> TLD2.ULTRADNS.NET
> 5 : TLD4.ULTRADNS.org <> TLD3.ULTRADNS.org
>
> Hope that helps,
> Jacques
>
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
compare-object PowerShell
compare object PowerShell
Compare-Object and Get the name of object/File? PowerShell
Testing object arrays using Compare-Object and -contains PowerShell
Adding canonical aliases for Compare-Object, Measure-Object, New-Object PowerShell


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