Powershell 如何找到两个数组之间的差值以及第三个数组的返回值?

Powershell 如何找到两个数组之间的差值以及第三个数组的返回值?,powershell,Powershell,我想比较两个数组,并使用差分值从另一个数组返回另一个值 通过这段代码,我可以确定$array1中的“b”不包含在$array2中。但是,我不知道如何在$array3中将“b”链接到2并返回该值 $array1 = (@("a","b","c")) $array2 = (@("a","c")) $array3 = (@(1,2,3)) # 1 should be linked to "a", 2 to "b" and 3 to "c" $array1 | ForEach-Object {If

我想比较两个数组,并使用差分值从另一个数组返回另一个值

通过这段代码,我可以确定$array1中的“b”不包含在$array2中。但是,我不知道如何在$array3中将“b”链接到2并返回该值

$array1 = (@("a","b","c"))
$array2 = (@("a","c"))
$array3 = (@(1,2,3))  # 1 should be linked to "a", 2 to "b" and 3 to "c"

$array1 | ForEach-Object {If ($_ -notin $array2) {$_}}
我感谢你的帮助


谢谢。

像这样的方法应该可以:

$index = 0..($array1.Count-1) | Where-Object { $array1[$_] -notin $array2 }
if ($index) { $array3[$index] }

像这样的方法应该会奏效:

$index = 0..($array1.Count-1) | Where-Object { $array1[$_] -notin $array2 }
if ($index) { $array3[$index] }

您可以使用compare对象

$array1 = @("a","b","c")
$array2 = @("a","c")
$array3 = @(1,2,3)  # 1 should be linked to "a", 2 to "b" and 3 to "c"

$diff = Compare-Object -ReferenceObject $array1 -DifferenceObject $array2  -PassThru

$diff | ForEach-Object { if($array1.Contains($_)){
       write-host $array3[$array1.IndexOf($_)]
   }
}

您可以使用compare对象

$array1 = @("a","b","c")
$array2 = @("a","c")
$array3 = @(1,2,3)  # 1 should be linked to "a", 2 to "b" and 3 to "c"

$diff = Compare-Object -ReferenceObject $array1 -DifferenceObject $array2  -PassThru

$diff | ForEach-Object { if($array1.Contains($_)){
       write-host $array3[$array1.IndexOf($_)]
   }
}

您的代码中有一个输入错误。将$array1[$i]替换为$array1[$\u]。谢谢你的提醒。修正了。你的代码有一个输入错误。将$array1[$i]替换为$array1[$\u]。谢谢你的提醒。固定的。