My Assistant
![]() ![]() |
|
|
Feb 1 2012, 02:56 PM
Post
#1
|
|
|
UtterAccess Guru Posts: 625 |
How would one loop through an array but start at a different point each iteration? I have an array:
1 2 3 4 5 and when I loop I want the output to be: 1 2 3 4 5 2 3 4 5 1 3 4 5 1 2 Is there some way to do this? |
|
|
|
Feb 1 2012, 02:58 PM
Post
#2
|
|
|
UtterAccess VIP Posts: 8,166 From: Pacific NorthWet |
One possibility ...
If you have a variable in your code that holds the "StartingPoint", you could increment it by 'n' each time the loop completed. That way, the next time through, the loop would start at another point. To achieve the "wrap-around" it looks like you're after, what about the idea of using modulus arithmetic? |
|
|
|
Feb 1 2012, 03:00 PM
Post
#3
|
|
|
UtterAccess Guru Posts: 731 From: North of the 49th Parallel |
Is there a method to that madness, or are you pulling those numbers out of thin air?
It looks like you're looping back around in the array? Can you set a known variable, run the array up to your limit, start it again at the beginning and back up to the known variable? It might take a loop and a couple if statements but you should be possible. |
|
|
|
Feb 1 2012, 03:21 PM
Post
#4
|
|
|
UtterAccess Guru Posts: 625 |
Right, I am just looping. That's what I'm trying to figure, is there some way to repeat with a different starting point?
|
|
|
|
Feb 1 2012, 03:34 PM
Post
#5
|
|
|
UtterAccess Addict Posts: 170 |
Like Jeff and Riley were saying you need to create a variable to hold the starting point. Remember the first field in an array starts at 0 and ends at # of fields - 1. Then loop through from the starting point to the end of the array. Then loop from the beginning of the array to starting point - 1. Something like this should work. Also do you know how many records the array will contain or is it dynamic?
CODE Dim myArray() As String Dim startPoint As Double Dim endPoint As Double Dim i As Double 'set initial start point to 0 startPoint = 0 'set initial end point to the final element in the array endPoint = UBound(myArray) 'loop through the array with every possible start point While startPoint <= UBound(myArray) 'loop through first iteration starting at 0 If startPoint = 0 Then For i = startPoint To endPoint 'RUN YOUR CODE HERE Next i End If 'loop through sucessive iterations until the final element in the array If startPoint > 0 and startPoint < UBound(myArray) 'set end point equal the element preceding the start point endPoint = startPoint - 1 'loop from start point to the end of the array For i = startPoint To UBound(myArray) 'RUN YOUR CODE HERE Next i 'loop from beginning of array to end point For i = LBound(myArray) To endPoint 'RUN YOUR CODE HERE Next i End If 'loop through final iteration starting at last element in array If startPoint = UBound(myArray) 'RUN CODE USING myArray(startPoint) 'set end point equal the element preceding the start point endPoint = startPoint - 1 'loop from first element in array to end point For i = LBound(myArray) To endPoint 'RUN YOUR CODE HERE Next i End If startPoint = startPoint + 1 Wend This is just off the top of my head and completely untested. To get the correct values you may need to change some of the For statements or = statements to plus or minus 1. You may need to change some of <= to < or other things along those lines. Use Debug.Print to see what elements are being access through each iteration of the array. Hope this helps This post has been edited by Cosmichighway: Feb 1 2012, 03:53 PM |
|
|
|
Feb 1 2012, 04:26 PM
Post
#6
|
|
|
UtterAccess Guru Posts: 625 |
Hmmm, I'm still working on this...not that it doesn't work but I'm trying to find a more elegant solution
|
|
|
|
Feb 1 2012, 04:43 PM
Post
#7
|
|
|
UtterAccess Guru Posts: 625 |
I should have been clearer....I have a loop and a loop here...the inner loop is printing the number list, the outer is doing the iterations.
|
|
|
|
Feb 1 2012, 04:56 PM
Post
#8
|
|
|
UtterAccess Addict Posts: 170 |
Is the array simply a list of consecutive numbers? If that is the case then you probably don't need to use an array at all. Provide what code you have, so we can better provide a possible solution.
|
|
|
|
Feb 1 2012, 05:08 PM
Post
#9
|
|
|
UtterAccess Guru Posts: 625 |
No, unfortunately it's not just consecutive numbers. It's a series of student numbers
|
|
|
|
Feb 1 2012, 05:19 PM
Post
#10
|
|
|
UtterAccess Guru Posts: 625 |
I think I got it, with a slightly more elegant solution:
CODE Function rotate_array(array_to_rotate() As Integer, startpos As Integer) As Variant Dim new_array() As Integer Dim counter As Integer Dim newpos As Integer Dim arr As String ReDim new_array(UBound(array_to_rotate, 1), 7) arr = "" newpos = startpos + 1 'MsgBox "rotate_array called with array size " & UBound(array_to_rotate, 1) 'MsgBox UBound(new_array, 1) For counter = 1 To UBound(array_to_rotate, 1) If newpos > 20 Then newpos = newpos - 20 arr = arr & counter & "-" & newpos & vbCrLf newpos = newpos + 1 Next MsgBox arr rotate_array = array_to_rotate End Function This will just print the new ordering of the array and return the original, but slight modification will return the reordered array. This post has been edited by kruuth: Feb 1 2012, 05:20 PM |
|
|
|
Feb 1 2012, 05:24 PM
Post
#11
|
|
|
UtterAccess VIP Posts: 8,166 From: Pacific NorthWet |
Wondering "why"? Is this just an exercise, or will you be making some use of the results? If so, by letting us in on what your eventual goal is, you may get new suggestions...
|
|
|
|
Feb 1 2012, 05:36 PM
Post
#12
|
|
|
UtterAccess Guru Posts: 625 |
Rotating a set of students on teams.
|
|
|
|
Feb 1 2012, 05:40 PM
Post
#13
|
|
|
UtterAccess VIP Posts: 8,166 From: Pacific NorthWet |
Thanks for the additional information.
So, to paraphrase, you're assigning student to teams, and want to 'rotate' that assignment. |
|
|
|
Feb 1 2012, 07:56 PM
Post
#14
|
|
|
UtterAccess Guru Posts: 625 |
More or less, yes.
In theory this would work with any array. |
|
|
|
Feb 2 2012, 10:07 AM
Post
#15
|
|
|
UtterAccess Addict Posts: 170 |
Well, if that is what you are trying to do you may want to approach this differently. As long as the students values remain in the same order as elements in the array, even changing the starting position may still give you fairly similar teams.
Instead you may want to look at this approach: -Create and populate your array. -Set up a random number generator to pick numbers from the Lbound (0) to UBound values of the array - randNumber = int((UBound(myArray) + 1) * rnd) -Use that random number to select an element from the array and store that value elsewhere say in an array called RedTeam -Set the value of the element that was just selected using the random number equal to the value of the last element in the array (this removes the possiblility of duplicates) -ReDim the array removing the last element -Loop through and select another random number and place the value of that element in seperate team array. -Continue this process until the array is empty using as many team arrays as you would like. This will give you mostly random teams every time you call this procedure. This post has been edited by Cosmichighway: Feb 2 2012, 10:07 AM |
|
|
|
Feb 2 2012, 10:54 AM
Post
#16
|
|
|
Utter A-fishin'-ado Posts: 17,723 From: Cincinnati, Ohio, USA . . . ><((((°> |
An alternative approach:
CODE Public Function PrintVariations()
Dim MyArray() Dim i As Long Dim j As Long Dim k As Long 'AssignArray MyArray = Array(1, 2, 3, 4, 5) For i = 0 To UBound(MyArray) For j = i To UBound(MyArray) + i k = j If k > UBound(MyArray) Then k = k - ((UBound(MyArray) + 1)) End If Debug.Print MyArray(k) Next j Debug.Print "Next variation" Next i End Function |
|
|
|
![]() ![]() |
|
Go to Top · Lo-Fi Version | Time is now: 19th May 2013 - 09:36 AM |