How to build a dynamic loop? Recursion?

skyyakal

Well-known member
Joined
Oct 3, 2003
Messages
74
I have arrays with the following characteritics:
The number of arrays is varying depending on the values, that an users enters on a form.
The length of each array is also different each time they are created.

I need a recombination of all values saved in these arrays, which would mean....

Example: If there are 3 Arrays with the length of 4 Values, 3 Values and 1 Value, I need to generate the following code:

Code:
FOR array1=1 To 4
FOR array2=1 To 3
FOR array3=1 To 1
...
NEXT
NEXT
NEXT
As a result I need the following.
Code:
If
Array1 has values: 1, 11, 111, 1111
Array2 has values: 2, 22, 222
Array3 has values: 3

At the end I need all the values above recombined in single lines in a text file like that:
[CODE]
3; 2; 1
3; 22; 1
3; 222; 1
3;2;11
3;22;11
3;222;11
3; 2; 111
3; 22; 111
3; 222; 111
3;2;1111
3;22;1111
3;222;1111
there any METHOD that builds/writes VB code?

I thought of the following solution:
Generating the VB code as Text and write it to a file. Then include this file at the appropriate place in my programm. But here I cannot find the include method. Would anyone know, what is a syntax for an "INCLUDE"-Method?

Second solution: Any idea about the recursion?

Thank you everybody in advance!!!
 
Some guys asked me to post the results, if I would solve the problem. Sine the previous post was closed by administrator because of a fight with an American, I posted the problem and the answer once more.

Here is the code for my N to M Array permutation.
Sorry for no comments in the code, I was in the harry. If someone has got questions just write.
CardValueArray is defined somewhere esle and is a structure array, wich contains arrays of different length with values I would like a permutation on.

Any comments are appreciated!!!

Code:
Public Sub Permutation()
        Dim Counter, InnerCounter, LeftBound, SetBackLoop, ControlSum, ArrayUpperBound, GreenLight, Move, Zeroing, CheckNextNonMaxPos, LeftBoundZeroing As Integer

        Dim PermutationVar As String
        Dim CounterPosition(), MaxPosition() As Integer
        ArrayUpperBound = CardValueArray.GetUpperBound(0) - 1

        ReDim CounterPosition(ArrayUpperBound)
        ReDim MaxPosition(ArrayUpperBound)


        Setting the boundaries for the Counters and the initial Counter values
        For Counter = 0 To ArrayUpperBound
            MaxPosition(Counter) = CardValueArray(Counter + 1).Value.GetUpperBound(0) - 1
            CounterPosition(Counter) = 0
        Next

        GreenLight = 1
        LeftBound = 0

        Do While (GreenLight = 1)
            Move = 1

            Writing a Permutation
            For Counter = 0 To ArrayUpperBound
                PermutationVar = PermutationVar & Convert.ToString(CardValueArray(Counter + 1).Value(CounterPosition(Counter))) & ";"
            Next
            Console.WriteLine(PermutationVar)
            PermutationVar = ""


            Moving the Counters
            If (CounterPosition(0) = MaxPosition(0)) Then
                LeftBoundZeroing = 1
                For Counter = 0 To ArrayUpperBound
                    ControlSum = MaxPosition(Counter) - CounterPosition(Counter)
                    If (ControlSum <> 0 And LeftBoundZeroing = 1) Then
                        LeftBound = Counter
                        LeftBoundZeroing = 0
                    Else
                        ControlSum = 0
                        For InnerCounter = 0 To ArrayUpperBound
                            ControlSum = ControlSum + (MaxPosition(InnerCounter) - CounterPosition(InnerCounter))
                        Next
                        If (ControlSum = 0) Then
                            Exit Do
                        End If
                    End If
                Next
            End If
            Zeroing = 1
            For Counter = ArrayUpperBound To LeftBound Step -1
                If (CounterPosition(Counter) = MaxPosition(Counter) And Zeroing = 1) Then
                    ControlSum = 0
                    For InnerCounter = Counter To ArrayUpperBound
                        ControlSum = ControlSum + (MaxPosition(InnerCounter) - CounterPosition(InnerCounter))
                    Next
                    If (ControlSum = 0) Then

                        For InnerCounter = ArrayUpperBound To LeftBound Step -1

                            CheckNextNonMaxPos = MaxPosition(InnerCounter) - CounterPosition(InnerCounter)

                            If (CheckNextNonMaxPos <> 0 And Zeroing = 1) Then
                                CounterPosition(InnerCounter) = CounterPosition(InnerCounter) + 1
                                For SetBackLoop = (InnerCounter + 1) To ArrayUpperBound
                                    CounterPosition(SetBackLoop) = 0
                                Next
                                Move = 0
                                Zeroing = 0
                            End If
                        Next
                    End If
                End If
            Next
            If (Move = 1) Then
                CounterPosition(ArrayUpperBound) = CounterPosition(ArrayUpperBound) + 1
            End If

        Loop

    End Sub
 
Back
Top