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?
Technology Tips and News
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?
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.
Can you throw some light on it pls
You haven't posted any code yet.
$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
i am yet to try , just started off with compare-object .
Post back when you have and we can help you from there.
$mail3 = $mail1 + $mail2 | select -unique
got this working with this, do u think this is a good idea?
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.