## RPGLE arrays

6940 pts.
Tags:
Arrays
RPG
RPGLE
Please provide me the solution or at least a hint to this problem. Lets Suppose I have an array having 4 elements ( A, B, C, D). I would like to have all the possible combination of the 4 elements with no repetition (i.e. in mathematical terms 4C1 + 4C2 + 4C3 + 4C4 = 15). How can we do this in RPGLE? Please reply ASAP as my whole project delivery is stuck up because of this requirement. And please provide me a generic solution as the array can have maximum of 10 elements. Thanks in Anticipation. Best Regards. Moderator Note: This question was submitted to our moderator mailbox.

Thanks. We'll let you know when a new response is added.

Sorry, you are right, I did miss that one 4c4. I still think the link is usefull. It clearly illustrates the pattern. It would be farly simple to program I think. if you had 4 elemts say a,b,c, and d. then you would start with the first one A and go B,C,D, then increment a counter and start with the second one B and add C and D then the last one woud be CD. that would be taking them two at a time, then do it three at a time then the last time is four at a time, then so on.. you would need some do loops or FOR loops,… seems fairly straight forward. Wish I had time to program it, sounds like an intresting exercise in using loops.

## Discuss This Question: 10 Replies

Thanks. We'll let you know when a new response is added.
• I would make it more specific. Suppose we are having an array with names: Karan Julie Mark Robert Since there are 4 elements in the array for my further requirement I need to have all the non-repetitive combination of these Four names. Going with mathematical formula for Combination we have the following output for 4 elements i.e. NC1 + NC2 + NC3 + NC4........NCN or 2**n - 1 Which for our case comes out to be 15. So there could be 15 combination of four names in the array. Like if we consider all the 4 elements for Combination we have Karan+Julie+Mark+Robert as one Combination which is equivalent to 4C4. And if we consider 3 elements for Combination we wil have 4 combinations: Karan+Julie+Mark Karan+Julie+Robert Julie+ Mark+Robert Karan + Mark + Robert Which is equivalent to 4C3. And if we consider 2 elements for Combination we wil have 6 combinations: Karan+Julie Julie+Robert Mark+Robert Julie + Mark Karan + Mark Karan + Robert Which is equivalent to 4C2. And if we consider 1 elements for Combination we wil have 4 combinations: Karan Julie Mark Robert Which is equivalent to 4C1. So Sum of all these combination comes out to be 15. For this we need to have a generic logic for the combinations of N number of elements in the array. Hope this will Help!!
report
• Do you want 2**n - 1 the 15 in your example or, which seems more likely the 15 answers.
report
• if it is the combinations of names you need rather than just the number 15, then here is a link to a really good explanation of how to get the different combinations. it includes an algorithm and enough informations on the patterns that you should be able to produce some RPG code to do it. It doesn't look too difficult.
report
• oops, I forgot to include the link, here it is http://www.codeguru.com/cpp/cpp/algorithms/combinations/article.php/c5117/
report
• come to think of it... how did you get 15? 4+6+4 is 14
report
• Thanx.. But I could not find anything Substantial from the link. I think you missed reading one combination i.e.4C4 which is equal to 1. Please any other ideas/help you can give..that would be great... Thanks, Karan
report
report
• I haven't quite got this smothed out but
```D wrka            S             10    dim(10)                                  incoming array
D result          S            100    dim(100)                               array to be loaded and tested for uniqueness

numEl is position of first blank element - 1 (10 if no blank elements)
I'm sure this could be similified with recursion but it's 10 little code snipets like this one.
Then the number of unique entries is the first blank in result array -1
I think you need to sort wrka before you begin.

*--------- process 3 elements
C                   if        numEl > 2
C                   for       x   = 1 to numEl - 2
C                   for       y   = 2 to numEl - 1
C                   for       z   = 3 to numEl
C                   eval      wrkPos = %lookup(*blanks: result)
C
C                   eval      wrkStr  = wrka(x) + wrka(y) + wrka(z)
C                   if        %lookup(wrkStr : result) = *zero
C                   eval      result(wrkPos) = wrkStr
C                   endif
C                   endfor
C                   endif

C                   endsr```