Option Compare Database
Option Explicit
Dim result(1000) As String [COLOR="Red"]'to record the strings returned by the alogrithm[/COLOR]
Dim maxresult As Long [COLOR="red"]'to count the number of strings found[/COLOR]
[COLOR="Blue"]'{sub combine not shown here}[/COLOR]
Sub main() [COLOR="red"]'call the process[/COLOR]
Dim x As Long [COLOR="red"]'just used to display the result[/COLOR]
Dim s As String
maxresult = 0 [COLOR="red"]'to start - clear the combination count to zero[/COLOR]
[COLOR="red"]'now initiate the recursion
'the first term is the number of items to select
'the second term is the number of terms to select from
'the third term is a null string to start from[/COLOR]
[COLOR="red"]'eg[/COLOR]
Call combine(3, 7, "") [COLOR="red"]'selects 3 from 7 (7x6x5 / 1x2x3 = 35 combinations)[/COLOR]
[COLOR="red"]'or[/COLOR]
Call combine(5, 9, "") [COLOR="red"]'selects 5 from 9 (9x8x7x6x5 / 1x2x3x4x5 = 126 combinations)[/COLOR]
[COLOR="red"]'now show the results[/COLOR]s = ""
For x = 1 To maxresult
s = s & result(x) & vbCrLf
Next
MsgBox (maxresult & " permutations" & vbCrLf & vbCrLf & s)
End Sub
How’s recursion different from a loop? What makes your code recursive is I guess what I’m asking, I recognize a loop and that’s it)))))))))))recursion works by a procedure repeatedly calling itself.
Understand this partso - lets say you want to select 5 items from 7 so in this case the first value can only be any of item 1 thru 3 (starting with item 3 gives you ONLY combination 3,4,5,6,7 - you cant start with item 4 - becasue you cant get a set of 5 values starting with 4)
so to keep going we’d have 123 extending to 1234,1235 and 1236, right? And then 1234 becomes 12345, 12346 and 12347 and this give me my first 3 combinations, and then the program goes on to what?so, in this case you need to start with each of values 1,2,3 - which means the process calls itself, starting with the partial string "1", "2" and "3" in each case
so when it starts with a string of "1" the recursive procedure then next calls itself with each of the possible values that will work for the next term of the series - so this time start with "12", "13" and "14" - (again not "15", because you cant get a string of 5 values starting with "15". Then "12" extends itself to "123", "124" and "125" - and so on
Function combine(required As Long, max As Long, combo As String) As String
Dim x As Long
Dim startfrom As Long
Dim newstr As String
If Len(combo) = 0 Then
startfrom = 0
Else
startfrom = CLng(Right(combo, 1))
End If
For x = startfrom + 1 To ((max - required) + Len(combo) + 1)
newstr = combo & CStr(x)
[COLOR="Blue"]'now determine whether the recursion is finished - or whether we need to drop dpwn another level[/COLOR]
If Len(newstr) = required Then
[COLOR="Blue"]'if so store this result in the final array[/COLOR]
[COLOR="red"] MsgBox ("End sequence " & newstr)[/COLOR]
maxresult = maxresult + 1
result(maxresult) = combo
Else
[COLOR="Red"] MsgBox ("Examining Strings Starting With " & newstr)[/COLOR]
Call combine(required, max, newstr)
End If
Next
End Function
sub examinefiles(basefolder)
for each file in the basefolder
if it is a folder then
examinefiles (for the folder)
else
list this file
end if
next
end sub
recursion different to a loop
if you tried to set up a loop to do this, you would find it very awkward, as the loop would change depending on how many items you were trying to select and from what population. I can't think offhand how I would do it in that way (as you found) , but I am sure it is possible
you mean the only problem is that you won't know how to explain to the loop when to stop? i can't believe that i'm actually understanding everything you're saying)))))))))))))) i'm so hyped because i'm really new to vba, i know almost nothing and i think i understand everything you say
Function combine(required As Long, max As Long, combo As String) As String
Dim x As Long
Dim startfrom As Long
Dim newstr As String
If Len(combo) = 0 Then
startfrom = 0
Else
startfrom = CLng(Right(combo, 1))
End If
For x = startfrom + 1 To ((max - required) + Len(combo) + 1)
newstr = combo & CStr(x)
'now determine whether the recursion is finished - or whether we need to drop dpwn another level
If Len(newstr) = required Then
'if so store this result in the final array
MsgBox ("End sequence " & newstr)
maxresult = maxresult + 1
result(maxresult) = combo
Else
MsgBox ("Examining Strings Starting With " & newstr)
Call combine(required, max, newstr)
End If
Next
End Function
1
2a 2b
3a 3b 3c
4a 4b 4c 4d
5a 5b 5 c 5d 5e 5f 5g 5h
6a
sub examine_node (nodebase as node)
if node has children then
process this non-leaf node
for each child_node
'note this is generalised for trees with any number of children at any node
'the recursion - examine this sub tree ....
call examine_node(child_node)
next
else
process this leaf node
end if
sub examine_node (nodebase as node)
if node has children then
process this non-leaf node
for each child_node
call examine_node(child_node)
next
else
process this leaf node
end if
It's recursive because it calls itself. Dave has written a function called combine. However, within that function, he calls the function combine i.e. it's calling itself. See how "examine node" is repeated in the pseudo code.which part of it makes this code recursive? can you point to the line or the word?
Yes .so the fact that you can call the function inside yourself makes it recursive? and some languages don't allow that? is it really that simple?
Sub listdirectory(root As String)
Dim fname As String
Dim s As String
MsgBox ("Checking directory " & root)
s = ""
fname = Dir(root, vbNormal + vbDirectory)
If fname = "" Then Exit Sub ' no files in this folder
While fname <> ""
'if this file is a folder then call this procedure again to
'go down the next folder. Use getattr to see if this is a folder
If (GetAttr(root & fname) And vbDirectory) = vbDirectory Then
If fname <> "." And fname <> ".." Then
s = s & fname & " (subfolder) " & vbCrLf
[COLOR="Red"] 'comment these next two lines out to see it working normally[/COLOR]
MsgBox ("Examining subfolder " & root & fname & "\")
listdirectory (root & fname & "\")
End If
Else
s = s & fname & vbCrLf
End If
On Error GoTo fail
'this is the problem
'dir is not recursive
[COLOR="Red"]fname = Dir[/COLOR]
Wend
MsgBox ("Completed examination of " & root & vbCrLf & vbCrLf & _
"Results were: " & vbCrLf & vbCrLf & s)
Exit Sub
fail:
MsgBox ("Error: " & Err & " Desc: " & Err.Description & vbCrLf & vbCrLf & _
"Results were: " & vbCrLf & vbCrLf & s)
End Sub
Sub directory_main()
'include whatever folder you want to check here ...
listdirectory ("C:\")
End Sub