For some kind of game where it would be necessary to find anagrams from a handful of scattered letters, I ended up implementing a permutation algorithm to find all possible anagrams and filter them out if necessary for known letter positions ( -match fine, by the way). But for longer words, this turned out to be very error prone, since dropping a large list of gibberish does not really show the correct words that were hidden inside.
So I thought that if I had a large list of English words (it should be available somewhere), I could just cross the permutation list with the list of correct words and get (hopefully) all real words from the permutation list.
Since many operators in PS work differently with collections, I thought I could just do something like
$wordlist -contains $permlist
and get the intersection back. Unfortunately, this is not so simple. Other options I was thinking about are to -contains over one list and do -contains for each item:
$permlist | ? { $wordlist -contains $_ }
Maybe this will work, but very slowly, I think (especially when $wordlist is the result of gc wordlist.txt ). Or I could build a giant regex:
$wordlist -matches (($permlist | %{ "^$_`$" }) -join "|")
But this is probably not very fast either. I could also use findstr with over a gigantic regex, but this is simply wrong.
Are there any built-in solutions that I could use that are better than my attempts? Otherwise, I would probably put the list of words in the hash table and use the iterative -contains approach, which should be fast enough.
set powershell intersection
Joey
source share