Skip to content

Instantly share code, notes, and snippets.

@IISResetMe
Last active July 16, 2019 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save IISResetMe/57ce7b76e1001974a4f7170e10775875 to your computer and use it in GitHub Desktop.
Save IISResetMe/57ce7b76e1001974a4f7170e10775875 to your computer and use it in GitHub Desktop.
function Compare-Array
{
param(
[array]$Ref,
[array]$Diff
)
$max = [math]::Max($Ref.Length,$Diff.Length)
for($i = 0; $i -lt $max; $i++){
if($Ref[$i] -ne $Diff[$i]){
[pscustomobject]@{
Index = $i
Left = $Ref[$i]
Right = $Diff[$i]
}
}
}
}
function Compare-StringSet
{
param(
[string[]]$Ref,
[string[]]$Diff,
[switch]$CaseSensitive
)
$Comparer = if($CaseSensitive){
[System.StringComparer]::InvariantCulture
}
else {
[System.StringComparer]::InvariantCultureIgnoreCase
}
$Results = [ordered]@{
Left = @()
Both = @()
Right = @()
}
$temp = [System.Collections.Generic.HashSet[string]]::new($Ref, $Comparer)
$temp.IntersectWith($Diff)
$Results['Both'] = $temp
$temp = [System.Collections.Generic.HashSet[string]]::new($Ref,[System.StringComparer]::CurrentCultureIgnoreCase)
$temp.ExceptWith($Diff)
$Results['Left'] = $temp
$temp = [System.Collections.Generic.HashSet[string]]::new($Diff,[System.StringComparer]::CurrentCultureIgnoreCase)
$temp.ExceptWith($Ref)
$Results['Right'] = $temp
return [pscustomobject]$Results
}
@fatherjack
Copy link

Interesting start but doesnt show difference (left or right) on this trial

$one = @('tree','cat','dog', 'house')
$two = @('yellow','house','ball', 'dog')

Compare-Array -Ref $one -Diff $two

results :

Index Left  Right 
----- ----  ----- 
    0 tree  yellow
    1 cat   house 
    2 dog   ball  
    3 house dog 

"expectation" would be

Leftonly
Tree
Cat

RightOnly
Yellow
Ball

Same
Dog
House

@IISResetMe
Copy link
Author

@fatherjack I added a Compare-StringSet example as well:

$ref,$dif = @(
  ,@('a','b','c')
  ,@('b','c','d')
)
Compare-StringSet $ref $dif

Output:

Left Both   Right
---- ----   -----
{a}  {b, c} {d}

@fatherjack
Copy link

Fabulous!

Many thanks
J

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment