Results 1 to 10 of 10

Thread: [Resolved]Reorganising Arrays...

  1. #1

    Thread Starter
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765

    [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:
    1. Public RndTracker(19) As String
    2. Public TempArray(19) As String
    3. '---
    4.  
    5. IntCurrindex = tempint   'temp int is a random integer
    6.  
    7. For i = 0 To UBound(RndTracker)
    8.   If i = 0 Then
    9.     Temparray(0) = IntCurrindex
    10.     Temparray(1) = RndTracker(0)
    11.   ElseIf i > 0 Then
    12.     Temparray(i) = RndTracker(i - 1)
    13.   End If
    14. Next i
    15.  
    16. 'Copy back
    17. For i = 0 To UBound(Temparray)
    18. RndTracker(i) = Temparray(i)
    19. 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.

  2. #2
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    VB Code:
    1. For i=1 To UBound(RndTracker)
    2.   RndTracker(i-1)=RndTracker(i)
    3. Next i
    4. RndTracker(UBound(RndTracker))=0
    Instead of the last line you could Redim the array to one element less by:
    VB Code:
    1. 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!

  3. #3

    Thread Starter
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765
    Redim won't work with a fixed array?

  4. #4
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    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!

  5. #5

    Thread Starter
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765
    I think your code does the same as mine. It moves one item back, but then thats it...unless I've screwed up somewhere...

  6. #6
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    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!

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104
    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.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104
    Bummer, I gotta remember those tags, that looks like poo.

  9. #9

    Thread Starter
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765
    The tags are [ vbcode] [ /vbcode] with out the space before the "v" &"/"

    If you can't remember it, then atleast use the Quote tags, so that it stands out.

    Thanks guys, I figured out why I couldn't get your code going, something else was screwing up my value.

  10. #10
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    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
  •  



Click Here to Expand Forum to Full Width