# Permutations from Multiple Arrays - Counting Principle Problem

## Recommended Posts

I'm trying to create an array showing all permutations possible by using one value from each of multiple arrays (values are independent of one another).  I have successfully done this for a fixed number of arrays (5), but am looking to create code to be able to do this for varying numbers of arrays.  I've included that code below (just the relevant portion).

A simple real world example of this problem is determining bicycle lock combinations where the lock might have (5) dials with each dial having 0 to 9 as possible values.  Each dial's values represents one of the "multiple arrays" I referenced above.  As explained above, I have created code to do this for a fixed (5) dials, but would like to have a solution that could do this no matter how many dials there are.  Put simply, I am not sure how to dynamically change the number of For...Next loops for a number of dials other than 5 (which I can do manually).  Note: Please ignore that I have a fixed 5 sections of input boxes in the code below, as I've already created a dynamic solution for as many inputs as needed in another code file.

```AutoItSetOption('MouseCoordMode', 0)

#include <Array.au3>
#include <File.au3>
#include <Excel.au3>
#include <GUIConstantsEx.au3>
#include <FileConstants.au3>
#include <WinAPIFiles.au3>
#include <MsgBoxConstants.au3>

; Asks the user to enter variable upper limit, lower limit and increment value for each screen parameter.  The M in the password
; field indicates that empty string is not accepted and the 3 indicates that the
; responce will be at most 3 characters long.

;these three lines set the values for j criteria
Local \$screencriteria01_LOWERLIM = InputBox("User Input", "Enter Criteria 1 Lower Limit", "", " M4")
Local \$screencriteria01_UPPERLIM = InputBox("User Input", "Enter Criteria 1 Upper Limit", "", " M4")
Local \$screencriteria01_INCREMENT = InputBox("User Input", "Enter Criteria 1 Increment", "", " M4")
Local \$screencriteria01_NUMPARS = (\$screencriteria01_UPPERLIM - \$screencriteria01_LOWERLIM)/\$screencriteria01_INCREMENT + 1

;these three lines set the values for i criteria
Local \$screencriteria02_LOWERLIM = InputBox("User Input", "Enter Criteria 2 Lower Limit", "", " M4")
Local \$screencriteria02_UPPERLIM = InputBox("User Input", "Enter Criteria 2 Upper Limit", "", " M4")
Local \$screencriteria02_INCREMENT = InputBox("User Input", "Enter Criteria 2 Increment", "", " M4")
Local \$screencriteria02_NUMPARS = (\$screencriteria02_UPPERLIM - \$screencriteria02_LOWERLIM)/\$screencriteria02_INCREMENT + 1

;these three lines set the values for k criteria
Local \$screencriteria03_LOWERLIM = InputBox("User Input", "Enter Criteria 3 Lower Limit", "", " M4")
Local \$screencriteria03_UPPERLIM = InputBox("User Input", "Enter Criteria 3 Upper Limit", "", " M4")
Local \$screencriteria03_INCREMENT = InputBox("User Input", "Enter Criteria 3 Increment", "", " M4")
Local \$screencriteria03_NUMPARS = (\$screencriteria03_UPPERLIM - \$screencriteria03_LOWERLIM)/\$screencriteria03_INCREMENT + 1

;these three lines set the values for m criteria
Local \$screencriteria04_LOWERLIM = InputBox("User Input", "Enter Criteria 4 Lower Limit", "", " M4")
Local \$screencriteria04_UPPERLIM = InputBox("User Input", "Enter Criteria 4 Upper Limit", "", " M4")
Local \$screencriteria04_INCREMENT = InputBox("User Input", "Enter Criteria 4 Increment", "", " M4")
Local \$screencriteria04_NUMPARS = (\$screencriteria04_UPPERLIM - \$screencriteria04_LOWERLIM)/\$screencriteria04_INCREMENT + 1

;these three lines set the values for n criteria
Local \$screencriteria05_LOWERLIM = InputBox("User Input", "Enter Criteria 5 Lower Limit", "", " M4")
Local \$screencriteria05_UPPERLIM = InputBox("User Input", "Enter Criteria 5 Upper Limit", "", " M4")
Local \$screencriteria05_INCREMENT = InputBox("User Input", "Enter Criteria 5 Increment", "", " M4")
Local \$screencriteria05_NUMPARS = (\$screencriteria05_UPPERLIM - \$screencriteria05_LOWERLIM)/\$screencriteria05_INCREMENT + 1

; Display the result for script test purposes and to provide a breakpoint
;MsgBox(\$MB_SYSTEMMODAL, "Number of Inputs", \$screencriteria05_NUMPARS)

;Create array to include all iterations of screen definition
Local \$aRankArray[\$screencriteria05_NUMPARS * \$screencriteria04_NUMPARS * \$screencriteria03_NUMPARS * \$screencriteria02_NUMPARS * \$screencriteria01_NUMPARS + 1][1]

For \$n = 0 to ((\$screencriteria05_UPPERLIM - \$screencriteria05_LOWERLIM)/\$screencriteria05_INCREMENT)
For \$m = 0 to ((\$screencriteria04_UPPERLIM - \$screencriteria04_LOWERLIM)/\$screencriteria04_INCREMENT)
For \$k = 0 to ((\$screencriteria03_UPPERLIM - \$screencriteria03_LOWERLIM)/\$screencriteria03_INCREMENT)
For \$i = 0 To ((\$screencriteria02_UPPERLIM - \$screencriteria02_LOWERLIM)/\$screencriteria02_INCREMENT)
For \$j = 0 To ((\$screencriteria01_UPPERLIM - \$screencriteria01_LOWERLIM)/\$screencriteria01_INCREMENT)
\$mycount = 0
\$kv = \$n * \$screencriteria01_NUMPARS * \$screencriteria02_NUMPARS * \$screencriteria03_NUMPARS * \$screencriteria04_NUMPARS + \$m * \$screencriteria01_NUMPARS * \$screencriteria02_NUMPARS * \$screencriteria03_NUMPARS + \$k * \$screencriteria01_NUMPARS * \$screencriteria02_NUMPARS + \$i * \$screencriteria01_NUMPARS + \$j + 1
\$jv = 0

\$aRankArray[\$kv][\$jv] = \$j * \$screencriteria01_INCREMENT + \$screencriteria01_LOWERLIM & "," & \$i * \$screencriteria02_INCREMENT + \$screencriteria02_LOWERLIM & "," & \$k * \$screencriteria03_INCREMENT + \$screencriteria03_LOWERLIM & "," & \$m * \$screencriteria04_INCREMENT + \$screencriteria04_LOWERLIM & "," & \$n * \$screencriteria05_INCREMENT + \$screencriteria05_LOWERLIM
;_ArrayDisplay(\$aRankArray, "Rank Level J")
Next
;_ArrayDisplay(\$aRankArray, "Rank Level I")
Next
;_ArrayDisplay(\$aRankArray, "Rank Level K")
Next
;_ArrayDisplay(\$aRankArray, "Rank Level M")
Next
;_ArrayDisplay(\$aRankArray, "Rank Level N")
Next

_ArrayDisplay(\$aRankArray, "All Combinations")```

##### Share on other sites

Moved to the appropriate forum, as the DEV forum very clearly states:

Quote

Do not create AutoIt-related topics here, use AutoIt General Help and Support

We even bolded it

√-1 2^3 ∑ π, and it was delicious!

## Create an account

Register a new account