比较两个数组并获取不常见的值。

时间:2022-05-01 09:02:40

i wanted a small logic to compare contents of two arrays & get the value which is not common amongst them using powershell

我需要一个小的逻辑来比较两个数组的内容,并得到使用powershell并不常见的值。

example if

如果

$a1=@(1,2,3,4,5)
$b1=@(1,2,3,4,5,6)

$c which is the output should give me the value "6" which is the output of what's the uncommon value between both the arrays.

$c是输出应该给我的值是"6"这是数组之间的不寻常值的输出。

Can some one help me out with the same! thanks!

谁能帮我一个忙!谢谢!

6 个解决方案

#1


80  

PS > $c = Compare-Object -ReferenceObject (1..5) -DifferenceObject (1..6) -PassThru
PS > $c
6

#2


15  

Look at Compare-Object

看比较对象

Compare-Object $a1 $b1 | ForEach-Object { $_.InputObject }

Or if you would like to know where the object belongs to, then look at SideIndicator:

或者,如果你想知道物体在哪里,那么看看侧标:

$a1=@(1,2,3,4,5,8)
$b1=@(1,2,3,4,5,6)
Compare-Object $a1 $b1

#3


10  

比较两个数组并获取不常见的值。

$a = 1,2,3,4,5
$b = 4,5,6,7,8

$Yellow = $a | Where {$b -NotContains $_}

$Yellow contains all the items in $a except the ones that are in $b:

$黄色包含所有$a的项,除了$b:

PS C:\> $Yellow
1
2
3

$Blue = $b | Where {$a -NotContains $_}

$Blue contains all the items in $b except the ones that are in $a:

$Blue包含所有$b的项,除了$a的项:

PS C:\> $Blue
6
7
8

$Green = $a | Where {$b -Contains $_}

Not in question, but anyways; Green contains the items that are in both $a and $b.

没有问题,但不管怎样;绿色包含在$a和$b中的项目。

PS C:\> $Green
4
5

#4


4  

Your results will not be helpful unless the arrays are first sorted. To sort an array, run it through Sort-Object.

除非先对数组进行排序,否则结果将不会有帮助。要对数组进行排序,可以通过sort对象运行它。

$x = @(5,1,4,2,3)
$y = @(2,4,6,1,3,5)

Compare-Object -ReferenceObject ($x | Sort-Object) -DifferenceObject ($y | Sort-Object)

#5


2  

Try:

试一试:

$a1=@(1,2,3,4,5)
$b1=@(1,2,3,4,5,6)
(Compare-Object $a1 $b1).InputObject

Or, you can use:

或者,您可以使用:

(Compare-Object $b1 $a1).InputObject

The order doesn't matter.

的顺序并不重要。

#6


0  

This should help, uses simple hash table.

这应该会有所帮助,使用简单的哈希表。

$a1=@(1,2,3,4,5) $b1=@(1,2,3,4,5,6)


$hash= @{}

#storing elements of $a1 in hash
foreach ($i in $a1)
{$hash.Add($i, "present")}

#define blank array $c
$c = @()

#adding uncommon ones in second array to $c and removing common ones from hash
foreach($j in $b1)
{
if(!$hash.ContainsKey($j)){$c = $c+$j}
else {hash.Remove($j)}
}

#now hash is left with uncommon ones in first array, so add them to $c
foreach($k in $hash.keys)
{
$c = $c + $k
}

#1


80  

PS > $c = Compare-Object -ReferenceObject (1..5) -DifferenceObject (1..6) -PassThru
PS > $c
6

#2


15  

Look at Compare-Object

看比较对象

Compare-Object $a1 $b1 | ForEach-Object { $_.InputObject }

Or if you would like to know where the object belongs to, then look at SideIndicator:

或者,如果你想知道物体在哪里,那么看看侧标:

$a1=@(1,2,3,4,5,8)
$b1=@(1,2,3,4,5,6)
Compare-Object $a1 $b1

#3


10  

比较两个数组并获取不常见的值。

$a = 1,2,3,4,5
$b = 4,5,6,7,8

$Yellow = $a | Where {$b -NotContains $_}

$Yellow contains all the items in $a except the ones that are in $b:

$黄色包含所有$a的项,除了$b:

PS C:\> $Yellow
1
2
3

$Blue = $b | Where {$a -NotContains $_}

$Blue contains all the items in $b except the ones that are in $a:

$Blue包含所有$b的项,除了$a的项:

PS C:\> $Blue
6
7
8

$Green = $a | Where {$b -Contains $_}

Not in question, but anyways; Green contains the items that are in both $a and $b.

没有问题,但不管怎样;绿色包含在$a和$b中的项目。

PS C:\> $Green
4
5

#4


4  

Your results will not be helpful unless the arrays are first sorted. To sort an array, run it through Sort-Object.

除非先对数组进行排序,否则结果将不会有帮助。要对数组进行排序,可以通过sort对象运行它。

$x = @(5,1,4,2,3)
$y = @(2,4,6,1,3,5)

Compare-Object -ReferenceObject ($x | Sort-Object) -DifferenceObject ($y | Sort-Object)

#5


2  

Try:

试一试:

$a1=@(1,2,3,4,5)
$b1=@(1,2,3,4,5,6)
(Compare-Object $a1 $b1).InputObject

Or, you can use:

或者,您可以使用:

(Compare-Object $b1 $a1).InputObject

The order doesn't matter.

的顺序并不重要。

#6


0  

This should help, uses simple hash table.

这应该会有所帮助,使用简单的哈希表。

$a1=@(1,2,3,4,5) $b1=@(1,2,3,4,5,6)


$hash= @{}

#storing elements of $a1 in hash
foreach ($i in $a1)
{$hash.Add($i, "present")}

#define blank array $c
$c = @()

#adding uncommon ones in second array to $c and removing common ones from hash
foreach($j in $b1)
{
if(!$hash.ContainsKey($j)){$c = $c+$j}
else {hash.Remove($j)}
}

#now hash is left with uncommon ones in first array, so add them to $c
foreach($k in $hash.keys)
{
$c = $c + $k
}