I'm trying to figure out how to achieve deletion of an object inside an ArrayList when the object is a reference type.
Consider the following code:
$leftTable = Import-Csv -Delimiter ';' -Path "C:\temp\linux_left.csv"
[System.Collections.ArrayList]$rightTable = Import-Csv -Delimiter ';' -Path "C:\temp\linux_right.csv"
$finalTable = @()
foreach($line in $leftTable) {
$matched = Compare-Object -ReferenceObject $line -DifferenceObject $rightTable -IncludeEqual -ExcludeDifferent -Property 'asset.name','definition.id','definition.name','state'| Select-Object 'asset.name','definition.id','definition.name','state'
if($matched)
{
$finalTable += $matched
$rightTable.Remove($matched)
}
else
{
...
}
}
How can I make $matched matches structurally what matched in $rightTable ?
I know it has something to do with Object.Equals() method that is testing for reference equality when the Remove() method is trying to match what I want to delete, but I really don't see how can I make this working with code above?
Posting from a phone so can’t give a full description right now, but the sample below works on tio.run…
Output:
Basically it finds the objects in
$rightthat match each item in$leftand then creates a new array with those items filtered out…