Set Operations RRS feed

  • General discussion

  • I've seen several questions over time (not a lot) asking how to find everything in "A" that's in "B", how to find everything in "A" that's not in "B", how to find everything that's in "A" and "B", etc. The examples I've seen seem always to be expressed in TSQL. The ones that used Powershell almost always the "-in" or "-notin" operators, or iteration over two arrays. But these questions all have an answer in "set operations", and I found a script module that handles them neatly (and it's easily extensible if you want to include something like "symmetric difference" (or "disjunctive union").

    You can find the module here:


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Friday, February 21, 2020 9:16 PM

All replies

  • Sounds very useful.  I expect reviewing the code will also be informative.  I have used set theory to design code when a problem was solvable in set.  


    Friday, February 21, 2020 11:22 PM
  • The module uses Compare-Object, -contains, and -notcontains to do the work.

    I looked at Linq, and the .NET HashSet class, but I think the need to use type-specificity would be unwieldy, even though the operations might be faster.

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Saturday, February 22, 2020 3:43 AM
  • I looked at Linq a couple of years ago.  I will try and find the scripts.  It is not that hard to leverage in PS once loaded.  Too bad MS has not included syntax support for Linq,  I had thought of creating some CmdLets that would allow parsing of a SQL-like syntax string that would then be passed to a DLL as Linq.  SQL, by nature, is a better SET language.

    The most important thing is to define "set of what?" and how is the set structure; lists, objects, dictionaries.  Compare-Object is a good first-try at comparing sets but only knows about object collections.  It does equivalence well and congruence is possible.  SQL was designed to implement SET theory but only against tuples.

    The weakness in all is the grammar and lack of good support for more than two SETs and sunsets.  

    I thing Mathematica has good SET support and should use pure mathematical notation.


    Saturday, February 22, 2020 11:42 AM