#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    13
    Rep Power
    0

    Coldfusion String return combinations


    I will like to return a table/list of rows consist of all possible combination of a string. For example string "01-02-03-04":


    1. 01 - 02 - 03 - 04
    2. 02 - 03 - 04
    3. 01 - 03 - 04
    4. 01 - 02 - 04
    5. 01 - 02 - 03
    6. 01 - 02
    7. 01 - 03
    8. 01 - 04
    9. 02 - 03
    10. 02 - 04
    11. 03 - 04

    Any help will be much appreciated. Thks in advance.
  2. #2
  3. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,286
    Rep Power
    968
    I did it like this:

    Code:
    <cfscript>
    delimiter = "-";
    results = [];
    values = "01-02-03-04";
    valueArray = ListToArray( values, delimiter );
    
    for( i=1; i <= ArrayLen( valueArray ); i++ )
    {
    	currentValue = valueArray[i];
    	ArrayAppend( results, currentValue );
    	
    	for( j=1; j <= ArrayLen( valueArray ); j++ )
    	{
    		if( valueArray[i] != valueArray[j] )
    		{
    			currentValue = currentValue & delimiter & valueArray[j];
    			ArrayAppend( results, currentValue );
    		}	
    	}
    }
    
    WriteDump( results );
    </cfscript>
    Which dumps the array of all of the permutations:

    Code:
    array
    1	 01
    2	 01-02
    3	 01-02-03
    4	 01-02-03-04
    5	 02
    6	 02-01
    7	 02-01-03
    8	 02-01-03-04
    9	 03
    10	 03-01
    11	 03-01-02
    12	 03-01-02-04
    13	 04
    14	 04-01
    15	 04-01-02
    16	 04-01-02-03
    Last edited by kiteless; March 9th, 2011 at 12:45 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    13
    Rep Power
    0
    Your result is different from mine. Pls take note that
    "01-02-03-04" is the same as "02-01-03-04" which is redundant.

    1 01
    2 01-02
    3 01-02-03
    4 01-02-03-04
    5 02
    6 02-01
    7 02-01-03
    8 02-01-03-04
    9 03
    10 03-01
    11 03-01-02
    12 03-01-02-04
    13 04
    14 04-01
    15 04-01-02
    16 04-01-02-03
  6. #4
  7. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,286
    Rep Power
    968
    Er, you said you wanted all possible combinations of a string. "01-02-03-04" is NOT the same as "02-01-03-04". That's like saying "ABCD" is the same as "BACD". Which clearly it is not.

    If what I showed isn't exactly what you want, feel free to modify it to suit your purposes.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    13
    Rep Power
    0
    Originally Posted by kiteless
    Er, you said you wanted all possible combinations of a string. "01-02-03-04" is NOT the same as "02-01-03-04". That's like saying "ABCD" is the same as "BACD". Which clearly it is not.

    If what I showed isn't exactly what you want, feel free to modify it to suit your purposes.
    Forgive me if my description is not clear. Actually for "01-02-03-04", i only wanted the results as shown above.
  10. #6
  11. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,286
    Rep Power
    968
    In that case you're going to want to take a recursive approach instead. I'd use something like this as a starting point for the logical flow.
  12. #7
  13. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,286
    Rep Power
    968
    Also, out of curiosity, what do you want to do with this? It doesn't seem very useful.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    13
    Rep Power
    0
    Originally Posted by kiteless
    Also, out of curiosity, what do you want to do with this? It doesn't seem very useful.
    Actually i need to do it cos my company is awarded a project with some overseas lottery company. And i had been assigned to this project.
  16. #9
  17. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,286
    Rep Power
    968
    I see. Well, like I said, the link above outlines the logical approach for the recursive combination algorithm.

IMN logo majestic logo threadwatch logo seochat tools logo