Results 1 to 9 of 9

Thread: Removing a Specific Item from Array

Threaded View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2012
    Posts
    158

    Removing a Specific Item from Array

    Hey everyone..I could really use some help with removing a specific item from an array. Here is what I have so far..

    Right now I have two arrays. The first array holds a list of domain names that will be checked to see if they are available for registration. If one or more of those domains is available then they will be put into a second array and sent over to get registered. Once the domains in the second array are successfully registered I want to remove them from the first array so they are not checked for availability again (since I've just registered them)..

    My current code is suppose to copy the main array (with all domains) to a temporary array - remove the registered domain(s) from the second array and then copy that temporary array (now with removed elements) back over to the main array so it can start from the beginning. The problem is, it doesn't actually remove the correct elements..It simply removes the last domain in the array. What this means, is that if I check a list of 5 domains and the domain in position 3 gets registered - it won't remove it from the array. Instead, the domain in the last position will be removed.

    Code:
        Public Sub ArrayCompare()
            Dim tmpcnt As Integer
            Dim cnt1 As Integer
            Dim cnt3 As Integer
            Dim cnt4 As Integer
    
            cnt1 = 0
            cnt3 = 0
            cnt4 = 0
            'Find the location of the registered domains in the main array
            tmpcnt = registeredArray.Length
            While cnt3 <= mainArray.Length - 1
                'here it checks the mainArray to see if it containts the registeredArray domain
                While cnt4 <= availablecnt
                    If InStr(mainArray(cnt3), registeredArray(cnt4)) = 0 Then
                        'do nothing
                    ElseIf InStr(mainArray(cnt3), registeredArray(cnt4)) > 0 Then
                        TempArray1 = mainArray
                        RemoveItem2()
                        mainArray = TempArray1
                        If tmpcnt = 1 Then
                            Exit Sub
                        End If
                    Else
                        txtLog.AppendText("Error With ArrayCompare: " & registeredArray(cnt4) & vbCrLf)
                    End If
                    cnt4 = cnt4 + 1
                End While
                cnt4 = 0
                cnt3 = cnt3 + 1
            End While
            ReDim Preserve mainArray(UBound(mainArray) - 1)
    
        End Sub
    and

    Code:
        Public Sub RemoveItem2()
            Dim TempDelElement As String
            For Me.cnt2 = 0 To DeleteArray.Length - 1
                TempDelElement = DeleteArray(cnt2)
                cnt1 = 0
                While cnt1 < TempArray1.Length - 1
                    If TempDelElement = TempArray1(cnt1) Then
                        RemoveElement()
                    End If
                    cnt1 = cnt1 + 1
                End While
            Next
        End Sub
    and

    Code:
        Public Sub RemoveElement()
            Dim I As Integer
            For I = cnt1 To UBound(TempArray1) - 1
                TempArray1(I) = TempArray1(I + 1)
            Next I
        End Sub
    Last edited by digitaldrew; May 13th, 2013 at 07:28 PM.

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