recursive function

acorkum

New Member
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.
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
 

acorkum

New Member
Here's what I started with...
Code:
def var myarray as char extent 4 init ["1","2","3","4"].             
def var list2 as char.                                                   
def var cnt as int init 4.                                               
def var x as int.                                                        
                                                                         
function chkCombos returns int                                           
    (input pos as int, input list as char):                              
                                                                         
    do while pos <= cnt:                                                 
                                                                         
        if list = "" then                                                
            list2 = myarray[pos].                                        
        else                                                             
            list2 = list + "," + myarray[pos].                           
                                                                         
        display list2 format "x(20)" with no-labels no-box. 
        chkCombos(pos + 1,list2).                      
        return x.                                      
    end.                                               
    return x.                                          
end.                                                   
                                                       
x = chkCombos (1,"").
Output is...
1
1,2
1,2,3
1,2,3,4

I am returning x with nothing in it, but later will return the list and sum of the values.

Thanks.
 

Marian EDU

Member
check this... you'll have the output you're looking for in clipboard (CTRL+V) ;)

you clean up the code and make it look nicer :)

Code:
def var myarray as char extent 4 init ["1","2","3","4"].             
                                                   
function chkCombos returns int                                           
    (INPUT arr AS CHAR EXTENT, input pos as int, input list as char):                              
    def var list2 as char.

    do while pos <= EXTENT(arr):                                                 
                                                                         
        if list = "" then                                                
            list2 = arr[pos].                                        
        else                                                             
            list2 = list + "," + arr[pos].                           
                                                                         
        PUT UNFORMATTED list2 SKIP. 
        pos = pos + 1.
        chkCombos(arr, pos, list2).                                                           
    end.                                               
    return 0.                                          
end.                                                   
                                                       
OUTPUT TO "CLIPBOARD".
chkCombos (myarray, 1, "").
OUTPUT CLOSE.
 
Top