-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCompareStringSet.ps1
More file actions
93 lines (67 loc) · 2.72 KB
/
Copy pathCompareStringSet.ps1
File metadata and controls
93 lines (67 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
function Compare-StringSet {
<#
.SYNOPSIS
Compare two sets of strings and see the matched and unmatched elements from each input
.DESCRIPTION
Compares sets of
.PARAMETER Ref
The reference set of values to be compared
.PARAMETER Diff
The difference set of values to be compared
.PARAMETER CaseSensitive
Enables a case-sensitive comparison
.EXAMPLE
$ref, $dif = @(
, @('a', 'b', 'c')
, @('b', 'c', 'd')
)
$Sets = Compare-StringSet $ref $dif
$Sets.RefOnly
$Sets.DiffOnly
$Sets.Both
This example sets up two arrays with some similar values and then passes them both to the Compare-StringSet function. the results of this are stored in the variable $Sets.
$Sets is an object that has three properties - RefOnly, DiffOnly, and Both. These are sets of the incoming values where they intersect or not.
.EXAMPLE
$ref, $dif = @(
, @('tree', 'house', 'football')
, @('dog', 'cat', 'tree', 'house', 'Football')
)
$Sets = Compare-StringSet $ref $dif -CaseSensitive
$Sets.RefOnly
$Sets.DiffOnly
$Sets.Both
This example sets up two arrays with some similar values and then passes them both to the Compare-StringSet function using the -CaseSensitive switch. The results of this are stored in the variable $Sets.
$Sets is an object that has three properties - RefOnly, DiffOnly, and Both.
Because of the -CaseSensitive switch usage 'football' is shown as in RefOnly and 'Football' is shown as in DiffOnly.
.NOTES
From https://gist.github.com/IISResetMe/57ce7b76e1001974a4f7170e10775875
#>
param(
[string[]]$Ref,
[string[]]$Diff,
[switch]$CaseSensitive
)
$Comparer = if ($CaseSensitive) {
[System.StringComparer]::InvariantCulture
}
else {
[System.StringComparer]::InvariantCultureIgnoreCase
}
$Results = [ordered]@{
RefOnly = @()
Both = @()
DiffOnly = @()
}
$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 = [System.Collections.Generic.HashSet[string]]::new($Ref, $Comparer)
$temp.ExceptWith($Diff)
$Results['RefOnly'] = $temp
#$temp = [System.Collections.Generic.HashSet[string]]::new($Diff, [System.StringComparer]::CurrentCultureIgnoreCase)
$temp = [System.Collections.Generic.HashSet[string]]::new($Diff, $Comparer)
$temp.ExceptWith($Ref)
$Results['DiffOnly'] = $temp
return [pscustomobject]$Results
}