Compare

Hi,

   i would like to compare 2 arrays in powershell and add the data in one array which is not present in the other, compare-object works good?

July 13th, 2015 2:04pm

Sure, that works for a starting point.
Free Windows Admin Tool Kit Click here and download it now
July 13th, 2015 2:06pm

any examples? is there anything like add the data in right which is not present in left to the left?
July 13th, 2015 2:07pm

any examples? is there anything like add the data in right which is not present in left to the left?
No, you'd need to do that yourself based on the output.
Free Windows Admin Tool Kit Click here and download it now
July 13th, 2015 2:08pm

Can you throw some light on it pls
July 13th, 2015 2:09pm

Can you throw some light on it pls

You haven't posted any code yet.

Free Windows Admin Tool Kit Click here and download it now
July 13th, 2015 2:10pm

$mail1 = mail@xyz.com, mail2@xyz.com

$mail2 = mail@xyz.com , mail3@xyz.com

i want to compare these 2 and add mail3@xyz.com to $mail1

July 13th, 2015 2:13pm

And what problems are you having? What have you tried?
Free Windows Admin Tool Kit Click here and download it now
July 13th, 2015 2:15pm

i am yet to try , just started off with compare-object .
July 13th, 2015 2:18pm

i am yet to try , just started off with compare-object .

Post back when you have and we can help you from there.

Free Windows Admin Tool Kit Click here and download it now
July 13th, 2015 2:20pm

$mail3 = $mail1 + $mail2 | select -unique

got this working with this, do u think this is a good idea?

July 13th, 2015 2:29pm

Sure, that will do the trick. How large are your source arrays?
Free Windows Admin Tool Kit Click here and download it now
July 13th, 2015 2:32pm

it may go upto 300 mail ids
July 13th, 2015 2:33pm

Those should be small enough for what you've posted to work for you.

Here's another method you could use:

Measure-Command {

    $a = @(1..300)
    $b = @(301..600)

    Compare-Object -ReferenceObject $a -DifferenceObject $b | ForEach {

        If ($_.SideIndicator -eq '=>') {

            $a += $_.InputObject

        }

    }

}

Measure-Command {

   $a = @(1..300)
   $b = @(301..600)

   $c = $a + $b | Select -Unique

}


Output:
Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 96
Ticks             : 968313
TotalDays         : 1.12073263888889E-06
TotalHours        : 2.68975833333333E-05
TotalMinutes      : 0.001613855
TotalSeconds      : 0.0968313
TotalMilliseconds : 96.8313

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 103
Ticks             : 1039414
TotalDays         : 1.20302546296296E-06
TotalHours        : 2.88726111111111E-05
TotalMinutes      : 0.00173235666666667
TotalSeconds      : 0.1039414
TotalMilliseconds : 103.9414

Using an ArrayList would likely be faster, but these are small enough for either of these methods to work.

Free Windows Admin Tool Kit Click here and download it now
July 13th, 2015 2:41pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics