|
-
Apr 28th, 2003, 07:04 AM
#1
Thread Starter
PowerPoster
[Resolved]Reorganising Arrays...
Well, I need to reorganise my arrays by being able to insert data in at the start, and push everything down which I have done with the following code,
VB Code:
Public RndTracker(19) As String
Public TempArray(19) As String
'---
IntCurrindex = tempint 'temp int is a random integer
For i = 0 To UBound(RndTracker)
If i = 0 Then
Temparray(0) = IntCurrindex
Temparray(1) = RndTracker(0)
ElseIf i > 0 Then
Temparray(i) = RndTracker(i - 1)
End If
Next i
'Copy back
For i = 0 To UBound(Temparray)
RndTracker(i) = Temparray(i)
Next i
The above code works well, but, I want to be able to go back through the array and reorganise it again.. which is where I've managed to get myself confused.
Basically I would need to remove the first item, and then replace it with the second item in the array, and I guess just make the last item in the array zero?
Should be an easy one for you guys (I hope), also just want to check that is the best way of doing it. 
Thanks in advance.
Last edited by Pc_Madness; Apr 29th, 2003 at 01:55 AM.
-
Apr 28th, 2003, 07:09 AM
#2
VB Code:
For i=1 To UBound(RndTracker)
RndTracker(i-1)=RndTracker(i)
Next i
RndTracker(UBound(RndTracker))=0
Instead of the last line you could Redim the array to one element less by:
VB Code:
Redim Preserve RndTracker(UBound(RndTracker)-1)
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Apr 28th, 2003, 07:14 AM
#3
Thread Starter
PowerPoster
Redim won't work with a fixed array?
-
Apr 28th, 2003, 07:15 AM
#4
You're right, sorry about that!
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Apr 28th, 2003, 07:20 AM
#5
Thread Starter
PowerPoster
I think your code does the same as mine. It moves one item back, but then thats it...unless I've screwed up somewhere...
-
Apr 28th, 2003, 07:28 AM
#6
I think it's the only way to do it!
Mine just doesn't use a second array.
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Apr 28th, 2003, 10:50 AM
#7
You can slightly speed up the Copy Back part of your code by just saying RndTracker=TempArray, rather than copying each element across.
Your code inserts at the beginning, and copies the first 19 items from the old array below it.
Opus's code moves the last 19 items up one slot, and puts a 0 in the last slot.
The one adds, the other takes away.
You could do your code without the temp array with something like this:
For i = UBound(RndTracker) to 1 Step -1
RndTracker(i)=RndTracker(i-1)
Next i
RndTracker(0) = <whatever that random number was>
That will be somewhat smaller and faster.
Sorry, I can't remember the tags for the code.
-
Apr 28th, 2003, 10:54 AM
#8
Bummer, I gotta remember those tags, that looks like poo.
-
Apr 29th, 2003, 01:52 AM
#9
Thread Starter
PowerPoster
-
Apr 29th, 2003, 01:54 AM
#10
Oh! great something else!!
At least we something to talk about.................
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|