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

Compare-object doesn't seem to work with Arrays

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 05-30-2007   #1 (permalink)
Chris Harris
Guest


 

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
Guest


 

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
Guest


 

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]
Guest


 

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
Guest


 

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
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
compare-object and hashtables VaDo PowerShell 3 08-06-2008 07:46 AM
Compare-Object and Get the name of object/File? akcorr PowerShell 5 06-19-2008 05:15 AM
Comparing files with compare-object Marco Shaw PowerShell 4 07-06-2007 11:21 AM
Testing object arrays using Compare-Object and -contains Alex K. Angelopoulos [MVP] PowerShell 2 08-31-2006 05:57 PM
Adding canonical aliases for Compare-Object, Measure-Object, New-Object Alex K. Angelopoulos [MVP] PowerShell 2 05-26-2006 07:58 AM


Update your Vista Drivers Update Your Drivers Now!!

Vistax64.com 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 2005-2008