Results 1 to 3 of 3

Thread: Clearing Array Elements

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Posts
    101

    Post

    Does anyone know of a simple, easy way to delete empty array elements?

    For example:

    champ(0) = "one"
    champ(1) = "two"
    champ(2) = ""
    champ(3) = "eight"
    champ(4) = ""
    champ(5) = "twenty"

    How can I get rid of champ(2) and champ(4) while leaving the rest of the data intact?

    Thanks,

    Chris

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,175

    Post

    Enjoy!
    Code:
        Dim champ() As String
        Dim intEntry As Integer
        Dim intEntry2 As Integer
        Dim intBlankEntry As Integer
        Dim bNoChanges As Boolean
        Dim intNonBlank As Integer
        Dim intHighEntry As Integer
        
        ReDim champ(5)
        champ(0) = "one"
        champ(1) = "two"
        champ(2) = ""
        champ(3) = "eight"
        champ(4) = ""
        champ(5) = "twenty"
        
        Do Until bNoChanges
            For intEntry = 0 To UBound(champ)
                bNoChanges = True
                ' Look fo a blank entry
                If champ(intEntry) = "" Then
                    intBlankEntry = intEntry ' Store the blank entry's location
                    ' Look for the next non-blank entry, starting at the entry that
                    ' follows the blank one
                    For intEntry2 = intEntry + 1 To UBound(champ)
                        If champ(intEntry2) <> "" Then
                            ' We found one
                            champ(intBlankEntry) = champ(intEntry2) ' Copy the non-blank value to the blank entry
                            champ(intEntry2) = "" ' Erase value we just copied
                            intHighEntry = intBlankEntry ' Store the "high water" mark
                            bNoChanges = False ' Record that we made a change
                            Exit For
                        End If
                    Next intEntry2
                End If
            Next intEntry
        Loop
        
        ' Do this only if you want to compress your array.
        ' If you don't want to do this then you don't need
        ' to record intHighEntry above
        ReDim Preserve champ(intHighEntry)
    
    End Sub

    ------------------
    Marty

    [This message has been edited by MartinLiss (edited 01-18-2000).]

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Posts
    101

    Post

    Thanks for the code Marty. But I don't think that I'll Enjoy doing this.

    Chris

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