powershell 比较多个数组 [英] powershell comparing multiple arrays
问题描述
我有一个需要比较多个数组的情况.有一个主数组包含子数组的所有元素以及一些额外的元素.
I've a case where multiple arrays need to be compared.There is one master array that contains all the elements that the child arrays have and also some extra elements.
在下面的例子中,$a
是主数组,$b
、$c
是子数组.我需要比较这些数组并获取 $a
中那些在 $b
和 $c
中不存在的额外元素的列表.实际上,就我而言,有 10 个子数组和一个主数组.
In below example, $a
is the master array and $b
, $c
are child arrays.
I need to compare these arrays and get the list of those extra elements in $a
that are not present in $b
and $c
.
Practically, in my case there are 10 child arrays and a master array.
$a="dhaw","roh","kohl","faf","abd","steyn","gupt","baz","kane","benn","brendn","joyc"
$b="roh","dhaw","kohl"
$c="steyn","abd","faf","roh","dhaw"
推荐答案
一个可行的解决方案可能是使用 -notcontains
运算符,如 arco444 建议的那样,循环遍历 $a
数组元素并检查它们是否至少包含在其他数组之一中.
A viable solution could be using -notcontains
operator as suggested by arco444, cycle through $a
array elements and check if they are contained at least in one of the other arrays.
这是一段代码
foreach($a_value in $a) {
if (($b -notcontains $a_value) -and ($c -notcontains $a_value)) {
"$a_value is extra"
}
}
这篇关于powershell 比较多个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!