Appending every element in an array to each other

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.

Any takers?

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?

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.

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.

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. :slight_smile:

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: 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

I think you missed your calling as a mathematician.

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. :slight_smile:

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.


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.