开发者

Getting Missing element from comparison of Array's using powershell [duplicate]

开发者 https://www.devze.com 2023-03-13 14:00 出处:网络
This question already has answers here: Closed 11 years ago. Poss开发者_开发技巧ible Duplicate: Comparing two arrays & get the values which are not common
This question already has answers here: Closed 11 years ago.

Poss开发者_开发技巧ible Duplicate:

Comparing two arrays & get the values which are not common

I wanted a logic to get uncommon items from an array, for example:

$a=@(1,2,3,4,5,6)
$b=@(1,2,3,4,5,7,9,10)

I want the output $c to be 6 which is the missing element in $b array, priority should be only given to the array contents of $a.

Can anyone please help me out with this?

Thanks!


Either empo's approach, or

$a1=@(1,2,3,4,5,8)
$b1=@(1,2,3,4,5,6)
Compare-Object $a1 $b1 | 
   Where-Object { $_.SideIndicator -eq '<=' } | 
   Foreach-Object { $_.InputObject }

returns 8


$c = $a | ? {!($b -contains $_)}

The priority will be given to the variable you "pipe".

0

精彩评论

暂无评论...
验证码 换一张
取 消