I had written a code in SCN Discussion started by kiran.premlal to calculate all possible permutations.
The code in this document has certain improvements like:
Let us look at some example sets.
| SET 1 | SET 2 | SET 3 | ||
| 54 | apple | 1 | ||
| 23 | orange | 2 | ||
| 101 | banana | 3 |
As we can see, sets can be of integers, strings, mixed data types and data can be unsorted.
However, for calculation of output, all we need is a set of indexes.
For index combination {2,3}, subset of SET 1 would be {23, 101}, whereas subset of SET 2 would be {orange, banana}
The code first calculates a list of index combinations, which can then be used to build data sets.
We look at SET 2 and see how output is being calculated.
| INDEX | DATA |
| 1 | apple |
| 2 | orange |
| 3 | banana |
We have 2 slots to fill, and first element can reserve a slot for starters.
Data sets are {apple, orange} and {apple, banana}.
Index set equivalents would be {1, 2} and {1, 3}.
Program would calculate index sets as:
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 2 | 3 |
| 3 | 1 |
| 3 | 2 |
When these indexes are replaced by their data equivalents, result would be:
| apple | orange |
| apple | banana |
| orange | apple |
| orange | banana |
| banana | apple |
| banana | orange |
Keeping above SET 2 as input data, we see how output is calculated.
For calculating singles and pairs, we have minimum set size = 1 and maximum set size = 2.
We first find all sets of size 1, and of size 2.
Output in index as well data form would be:
| 1 | apple | |||
| 2 | orange | |||
| 3 | banana | |||
| 1 | 2 | apple | orange | |
| 1 | 3 | apple | banana | |
| 2 | 3 | orange | banana |
For same input data, 6 permutation of pairs was calculated, whereas only 3 combination of pairs.
The dataset in program is build using characters starting from 'B'.
For dataset of size 3, program would build internal table of characters B, C and D.
| INDEX | DATA |
| 1 | B |
| 2 | C |
| 3 | D |
We specify in program selection the size of dataset, max/min size of set, and option to find permutation or combination.

Program would store resulting sets in internal table GT_SETS, whose record is capable of storing table of integers.

Although this program prints the resulting sets, minor changes can be done to return the results to another routine.
