I'm trying to create a permutation of a multidimensional array in classic asp (vbscript) and I'm seriously stuck. I've tried several functions of my own and also tried copying several php versions over, but I often end up with something that either goes into a buffer overflow / infinite recursion or I get results that are more like a combination than a permutation, if I understand the differences correctly.
Lets say it's for a shirt. The shirt can have colors, sizes, and styles. (The actual system allows for any number of "groups" of options (think color, size, etc) and also any number of options within each group (each particular size, each particular color,etc).
For example:
small med lg xl red blue green white pocket no-pocket
Note that the number of elements in either dimension of the array are unknown beforehand; also, not all second dimensions will have the same number of elements.
I need to iterate through each possible unique option that contains an option from each row. In this particular example, there would be 32 options (because I need to ignore results that have an empty value for any given option, since asp doesn't really handle a jagged array the way I would expect. So: small red pocket small red no-pocket small blue pocket small blue no-pocket etc.
Once I have this part done, I'll need to integrate it with some IDs from the database, but I'm fairly sure I can do that part on my own. It's the recursive function that's killing me.
Anyone able to point me in a good starting place or help me out? Any help is MUCH appreciated!
If you only have to worry about those four fixed categories, just use nested for loops.
If the number of categories may change, a recursive solution is easy to define:
Call with index=0 and permutation empty for best results (sources is an array of arrays containing your categories).
Example:
Generic solution in 20 lines!
To avoid problems of terminology: I wrote a small program:
and that's its output:
If that looks like a seed to a solution of your problem, just say so and I will post the code for the cOdoDemo class.
Code for cOdoDemo:
Some hints/remarks: Think of an odometer that genererates all combinations for 6 (7?) places/digits in numerical order. Now imagine an odometer that lets you specify a sequence/ordered set of 'digits'/words/items for each place/slot. This specification is done by aaItems.
This is the code for cPad, used in .run():
Sorry for the missing documentation. I'll try to answer all your question.