0

I would like to compare two arrays and use the differencing value to return another value from another array.

With this snippet I am able to determe that "b" from $array1 is not contained in $array2. However, I don`t know how to link "b" to 2 in $array3 and return that value.

$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) {$_}}

I appreciate your help.

Thanks.

2 Answers 2

1

You can use compare-object

$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($_)]
   }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Something like this should work:

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

2 Comments

There is a typo in your code. Replace $array1[$i] to $array1[$_]
Indeed. Thanks for the heads up. Fixed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.