Hello,
I am attempting to create a function that recursively creates all combinations of an array. The # elements in the array can vary. I have found a (what seems) simple function written in PHP that I am attempting to convert into Progress. The problem seems to be the pre-incrementing of 'pos' variable (and maybe more!). Here is the PHP code I am attempting to mirror.
The array would not need to be passed and I would pass the number of array elements. In the end I am trying to find totals of the array amounts, but getting the combinations is the current stumbling point. If someone could provide some assistance it would be appreciated. An alternative to this approach would also be welcome. My function experience is very limited... especially recursive where I have no experience!
Thanks,
Andrew
I am attempting to create a function that recursively creates all combinations of an array. The # elements in the array can vary. I have found a (what seems) simple function written in PHP that I am attempting to convert into Progress. The problem seems to be the pre-incrementing of 'pos' variable (and maybe more!). Here is the PHP code I am attempting to mirror.
Code:
<?php
function show_combinations ($array, $pos, $list) {
while ($pos < count($array)) {
if ($list == '')
$list2 = $array[$pos];
else
$list2 = $list . ',' . $array[$pos] ;
echo "$list2 <br />";
show_combinations($array, ++$pos, $list2);
}
}
$pos = 0;
$list = '';
$array = array(1,2,3,4);
show_combinations($array, $pos, $list);
?>
Output is...
1
1,2
1,2,3
1,2,3,4
1,2,4
1,3
1,3,4
1,4
2
2,3
2,3,4
2,4
3
3,4
4
The array would not need to be passed and I would pass the number of array elements. In the end I am trying to find totals of the array amounts, but getting the combinations is the current stumbling point. If someone could provide some assistance it would be appreciated. An alternative to this approach would also be welcome. My function experience is very limited... especially recursive where I have no experience!
Thanks,
Andrew