Results 1 to 10 of 10

Thread: How to delete an array element?

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2000
    Location
    Hong Kong
    Posts
    62

    Question

    I have an array. some of the elements in this array is empty. How can I kick away all those that is empty and the whole array can move upward? (i.e. pack all those are empty)
    Please Visit My WebCam!!
    http://www.hmcheung.com

  2. #2
    Addicted Member Quintonir's Avatar
    Join Date
    Mar 2001
    Location
    The Netherlands
    Posts
    155

    Lightbulb Possible

    That's possible, I'm a bit busy right now, but I'll make a sample code for you as soon as I can, and when the solution isn't already given.


    Quintonir

  3. #3
    Addicted Member Quintonir's Avatar
    Join Date
    Mar 2001
    Location
    The Netherlands
    Posts
    155

    Post The code

    Ok, here is the code. It's a simple example of how to shorten it, but it'll teach you the basics.

    ----------------
    Dim sArray(): ReDim sArray(1 To 10)
    Dim Count, Count2 As Long

    sArray(1) = "Hello"
    sArray(2) = ""
    sArray(3) = "My"
    sArray(4) = "Name"
    sArray(5) = "Is"
    sArray(6) = "Quintonir"
    sArray(7) = ""
    sArray(8) = "What"
    sArray(9) = "Is"
    sArray(10) = "Yours?"

    For Count = UBound(sArray) To LBound(sArray) Step -1
    If sArray(Count) = "" Then
    For Count2 = Count To UBound(sArray) - 1
    sArray(Count2) = sArray(Count2 + 1)
    Next

    sArray(UBound(sArray)) = ""
    ReDim Preserve sArray(1 To (UBound(sArray) - 1))
    End If
    Next

    ' Array fixed!
    ----------------

    By the way, if you know how to insert a VB code (some users can automate something) please tell me


    Quintonir

  4. #4
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Try this:

    Code:
    Dim sArray() As String
    
    Dim A As Long
    Dim Count As Long
    
    ReDim sArray(9)
    
    sArray(0) = "Hello"
    sArray(1) = "My"
    sArray(2) = ""
    sArray(3) = "Name"
    sArray(4) = "Is"
    sArray(5) = "Quintonir"
    sArray(6) = ""
    sArray(7) = "What"
    sArray(8) = "Is"
    sArray(9) = "Yours?"
    
    Count = 0
    For A = 0 To UBound(sArray, 1)
    	 If Not sArray(A) = "" Then
    		  sArray(Count) = sArray(A)
    	 
    		  Count = Count + 1
    	 End If
    Next
    Count = Count - 1
    
    If Count < 0 Then
    	 Erase sArray
    Else
    	 ReDim Preserve sArray(Count)
    End If
    
    'Code improved by vBulletin Tool (Save as...)

  5. #5
    Addicted Member Quintonir's Avatar
    Join Date
    Mar 2001
    Location
    The Netherlands
    Posts
    155

    Question VB Code

    Hiya Fox, could you please share the knowledge on how to add the code in Visual Basic format ??

  6. #6
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    See the last line in my code I use my vB Tool to coloize the code..
    But you may also use the [code] 'Code here [/code] -tags to get formatted code without colors..

  7. #7
    Addicted Member Quintonir's Avatar
    Join Date
    Mar 2001
    Location
    The Netherlands
    Posts
    155

    Lightbulb Thanks

    Thank you very much, that will improve people's understanding of my posted codes!

  8. #8
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    True true... unformatted code is worse than anything else

  9. #9
    Addicted Member Quintonir's Avatar
    Join Date
    Mar 2001
    Location
    The Netherlands
    Posts
    155

    Re-port code

    Ok, now that I have found out the new way of posting code:

    Code:
    Dim sArray(): ReDim sArray(1 To 10) 
    Dim Count, Count2 As Long 
    
    sArray(1) = "Hello" 
    sArray(2) = "" 
    sArray(3) = "My" 
    sArray(4) = "Name" 
    sArray(5) = "Is" 
    sArray(6) = "Quintonir" 
    sArray(7) = "" 
    sArray(8) = "What" 
    sArray(9) = "Is" 
    sArray(10) = "Yours?" 
    
    For Count = UBound(sArray) To LBound(sArray) Step -1 
    
      If sArray(Count) = "" Then
    
        For Count2 = Count To UBound(sArray) - 1 
          sArray(Count2) = sArray(Count2 + 1) 
        Next 
    
        sArray(UBound(sArray)) = "" 
        ReDim Preserve sArray(1 To (UBound(sArray) - 1)) 
    
      End If 
    
    Next 
    
    ' Array fixed!

  10. #10
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Wasn't my solution better?

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