Permutation Generation

JamesBowen

19+ years progress programming and still learning.
Hi all.

I have a small problem and for some unknown reason I just can't get my head around in coming up with a solution.

I need to come up with a function which will generate values of every permutation of a given "Letters" and "Length".

Example would be "ABCD" and the length is 8:

"AAAAAAAA"
"AAAAAAAB"
"AAAAAAAC"
"AAAAAAAD"
"AAAAAABA"
"AAAAAABB"
"AAAAAABC"
"AAAAAABD"
"AAAAAACA"
...........
"DDDDDDDD"

For the life of me, I just can't think how to do the code. I have found some PHP code but I coming up with problems in trying to convert PHP into ABL.

Can anyone help me out. Thanks.:biggrin:

James.

Here is the sample PHP code:
http://www.weberdev.com/get_example-3596.html
 
Sample, not perfect but could be something you can work with.

Code:
DEFINE VARIABLE vString AS CHARACTER   NO-UNDO.
DEFINE VARIABLE vNewStr AS CHARACTER   NO-UNDO.
DEFINE VARIABLE iLength AS INTEGER     NO-UNDO.
DEFINE VARIABLE iChgPos AS INTEGER     NO-UNDO.
DEFINE VARIABLE iStrLen AS INTEGER     NO-UNDO.

ASSIGN vString = "ABCD" 
    iLength = 8
    iStrLen = LENGTH(vString).

vNewStr = FILL (SUBSTRING(vString,1,1), iLength).
iChgPos = iLength. 
OUTPUT TO "c:\permutation.txt" .
DO iChgPos = 1 TO iLength :
  RUN pLoop01 (vNewStr, (iLength - iChgPos + 1)).
END.
OUTPUT CLOSE.

PROCEDURE pLoop01 :
    DEFINE INPUT PARAMETER cChgStr   AS CHAR NO-UNDO.
    DEFINE INPUT PARAMETER iPosition AS INT NO-UNDO.

    DEFINE VARIABLE iCtr01  AS INTEGER.
    DEFINE VARIABLE iCtr03  AS INTEGER.
    DEFINE VARIABLE iCtr04  AS INTEGER.

    IF iPosition <> iLength THEN
    DO:
        iCtr03 = 2.
         /* Replace the string with A */
         vNewStr = SUBSTRING(vNewStr,1,iChgPos) +
          SUBSTRING(vString,1,1) +
          SUBSTRING(vNewStr,iChgPos + 2).
    END.
    ELSE iCtr03 = 1.

    /* Change it with all different characters in the string */
    DO iCtr01 = iCtr03 TO iStrLen WITH FRAME a:
      vNewStr = SUBSTRING(cChgStr,1,iPosition - 1) +
            SUBSTRING(vString,iCtr01,1) +
            SUBSTRING(cChgStr,iPosition + 1).
      
      PUT iChgPos iPosition 
          iCtr01 " " vString " " cChgStr " " vNewStr SKIP.

      /* Recursive call */
      IF iPosition < iLength  THEN
      DO iCtr04 = (iPosition + 1) TO iLength:
         RUN pLoop01 (vNewStr, iCtr04).
      END.
    END.

END PROCEDURE.

HTH
 
Great. Thanks, it works a treat.
Now I can create my Bruteforce cracking database.
 
Back
Top