September 3, 2014 at 2:54 pm #18505
Let's say I've got an array like this:
$array = @(''this','and','that')
..and I want an array output like this:
$array = @("this","and","that","this,and","this,and,that","and,this","and,that","that,this","that,and")
I want to concatenate each element to each other with a comma-delimiter and also display the original elements. I want to create an array of strings that contains all combinations of all the original elements.
September 3, 2014 at 4:22 pm #18506
Yikes! Is this just for fun? What problem are you trying to solve?
Is the array always 3 dimensions? Or do you need something that will work with arbitrary-sized arrays?
September 3, 2014 at 5:46 pm #18509
It actually has a purpose. I'm trying to create a dynamic parameter and from a few elements in an array, valid options are everything I mentioned. It sounds like a text programming problem but has real-world implications.
September 3, 2014 at 6:50 pm #18510
That output is going to grow very large, very fast with the size of the input set. What you've described is every permutation of every combination. There are 2^n combinations of a set with n elements, and there are k! permutations of each individual combination of length k (where k will be somewhere between 1 and n). If you need to store all of this in memory as strings, you'll be somewhat limited in the number of strings (and in their length) that you can use before running into an OutOfMemory exception, and the code to generate all of the sets will also be quite slow, so you would want to cache the results somewhere.
It would probably be better to have your parameter accept an array of strings, use ValidateSet('this', 'and', 'that'), and have your function check for duplicates and join the array with commas at execution time, rather than trying to create one massive ValidateSet for a [string] parameter.
September 3, 2014 at 6:53 pm #18512
Yes, exactly. Permutations; that's the word I was thinking of. I'd have to check again but I believe there are 6 common elements so that'd be 2^6 elements ValidateSet() set. That's only 128 elements. I realize it's kind of crazy to do but I was just wondering if anyone had any quick ideas on how to do get it done. I wanted to try it out more as an academic exercise and if it actually worked well then just to leave it. 🙂
September 3, 2014 at 7:03 pm #18513
It would be 2^6 if you didn't care about the order and just wanted to generate combinations. You also want the permutations of each possible combination, so there would be 6 * 5 * 4 * 3 * 2 (6!) permutations of the set of 6, 5! permutations of each set of 5, and so on. I don't have the formula handy for the total number of values that would be in the ValidateSet, but it's quite a bit higher than 2^6. Still, that small of a set is probably doable without running into memory exceptions.
As luck would have it, some Google gave me a link to some CS professor's lecture on doing exactly this: http://www.cs.utexas.edu/users/djimenez/utsa/cs3343/lecture25.html. You'd have to adapt the code to PowerShell, and build strings instead of just printing them to the screen, but the basic ideas for generating combinations and permutations are there. Combining them in a powershelly way would look something like this:
Get-Combinations -Set $set | Get-Permutations
September 3, 2014 at 7:05 pm #18514
I think you missed your calling as a mathematician.
September 3, 2014 at 7:09 pm #18515
Nah, this is computer science stuff. I didn't finish my degree before I switched over to an IT career path, but I've at least been exposed to this kind of thing at some point or another. Enough to remember how to put together a reasonable Google search on it, at any rate. 🙂
September 4, 2014 at 10:05 am #18533
Would this work? The ValidateSet attribute will validate each item in the array. Callers of your function would then have to pass a list of values.
[ValidateSet('one','two','three','four','five','six')] [string] $DynamicParameter
September 4, 2014 at 1:18 pm #18538
Turns out this is going to be too big of a PITA just to validate some parameters in an obscure function that will probably rarely get used. I give.
You must be logged in to reply to this topic.