Heh, this was fun, ok I commented the code, basically what this is doing is recursively finding all the combinations of
digits number of digits using the numbers between
min and
max as possible values. What this allows us to do is create the array like I have above, then use the function to find all the possible combinations of array indexes (without repeating, that was the hard part):
Code:
<%
'Will return # delimited strings of comma delimited numbers
Function combos(min, max, digits)
Dim answerString, workingString
Dim workingArray
Dim outer, inner
'If there are more than 1 digit left to resolve
If digits > 1 Then
'recursively get all the right digit combinations
workingString = combos(min, max, digits-1)
'split the strings into an array so each array index will be a possible digit combination for digits-1
workingArray = Split(workingString,"#")
'outer is left most number
For outer = min to max
'loop through the strings returned for right side
For inner = 0 to UBound(workingArray)
'if this number hasn't been used in the right side yet
If InStr(workingArray(inner),outer) = 0 Then
'concatenate all the remaining possible outer values onto the right sides in strings that are # delimited
answerString = answerString & outer & "," & workingArray(inner) & "#"
End If
Next
Next
Else
'there is only 1 digit reminaing to resolve, simply concatenate 0 to 4 with #'s
For outer = min to max
answerString = answerString & outer & "#"
Next
End If
'return the result of this function
combos = left(answerString,len(answerString)-1)
End Function
%>
There's our function, now we need to call it. To test this out here is a small piece of code:
Code:
<table>
<%
Dim answers, answersArray
answers = combos(0,4,5)
answersArray = Split(answers,"#")
Response.Write "There are " & UBound(answersArray)+1 & " combinations.<br>"
Dim i
For i = 0 to UBound(answersArray)
Response.Write "<tr><td>"&i&"</td><td>"&answersArray(i)&"</td></tr>"
Next
%>
</table>
This sample code runs the function for array indexes 0 to 4 and wants 5 digit combinations back. The combinations are passed back like this:
0,1,2,3,4#0,1,2,4,3#0,1,3,2,4# etc...
So we can split on the # sign to get each individual set of indexes, then when we want to apply these to our array in the previous post we can split on the commas to retrieve the indexes. The only problem with this code is that it will not handle numbers higher than 9 for min and max and there is no error checking. If you enter less digits than appear between min and max you will have an error, for example:
combos(0,9,4) says make all the 4 digit combinations with the numbers between 0 and 9, this is legal.
combos(0,4,9) says make all the 9 digit combinations with the numbers between 0 and 4, this is not legal because there are only 5 numbers to choose from.
Now, in order to convert from the indexes to the numbers in our array (myArray from previous post with 5 user numbers in it):
Code:
<table>
<%
Dim myArray(4)
'we are assuming the previous page had a form with 5 inputs for the users number, txtNumber0, txtNumber1, txtNumber2, etc
Dim i
For i = 0 to 4
myArray(i) = i+5
Next
Dim answers, answersArray, digitCtr, digitArray
Dim tempIndex
answers = combos(0,4,5)
answersArray = Split(answers,"#")
Response.Write "There are " & UBound(answersArray)+1 & " combinations.<br>"
For i = 0 to UBound(answersArray)
'splits the comma delimited string for this combination
digitArray = Split(answersArray(i),",")
If i mod 5 = 0 Then Response.Write "<tr>"
Response.Write "<td>| "
For digitCtr = 0 to UBound(digitArray)
tempIndex = cInt(digitArray(digitCtr))
Response.Write myArray(tempIndex) & " "
Next
Response.Write "</td>"
If i mod 5 = 4 Then Response.Write "</tr>"
Next
%>
</table>
I substituted values in the array instead of the user values from Request.Form because this was complicated enough to require me to test it before posting. I think this may be a bit more than what you were after, but I enjoyed writing it.
-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
No more vacation for me
FAQ FAQ20-2863
= new Forums.Posting.General.GettingAnswers()