Results 1 to 14 of 14

Thread: Delete multiple elements (solved)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2001
    Location
    Liverpool, England
    Posts
    155

    Delete multiple elements (solved)

    Hello,

    How can I delete an element from an array if it has already occurred?

    e.g.

    1 example
    2 elements
    3 for
    4 example
    5 array

    Would become...

    1 example
    2 elements
    3 for
    4 array

    Thanks
    Last edited by sweevo; Mar 4th, 2002 at 07:20 AM.

  2. #2
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    either copy the array to a new array, or use a dynamic array...

    then you can redim it using the preserve keyword.

    i.e.
    VB Code:
    1. Redim Preserve aMyArray(3)
    will set the upperbound of the 1st dimension to 3 (4 elements), thus destroying the 5th element, but keeping the existing 4 - becuase you used the Preserve keyword
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2001
    Location
    Liverpool, England
    Posts
    155
    Thanks, but how can I work through the elements and delete reoccurring ones, assuming I don't know the contents?

  4. #4
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    well i think the best way would be to copy the elements accross to another array and as you copy each one accross, first check whether it already exists in the new array.
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  5. #5
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    Re: Delete multiple elements

    Originally posted by sweevo
    Hello,

    How can I delete an element from an array if it has already occurred?

    e.g.

    1 example
    2 elements
    3 for
    4 example
    5 array

    Would become...

    1 example
    2 elements
    3 for
    4 array

    Thanks
    This is not actually an easy thing to do ya know
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2001
    Location
    Liverpool, England
    Posts
    155
    Hmmm... I thought as much, because I couldn't find a way to do it. I did think that maybe I was missing something reasonably simple, as I often do, but evidently not.

    Darre, I'll give it a go.

    Cheers

  7. #7
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Add a reference to the microsoft scripting runtime library (Project > References), then use this code

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub removeDuplicates(ByRef arrName() As String)
    4.     Dim i As Long, tempArr() As String: ReDim tempArr(UBound(arrName))
    5.     Dim d As New Dictionary, n As Long
    6.     For i = 0 To UBound(arrName)
    7.         If Not d.Exists(arrName(i)) Then
    8.             d.Add arrName(i), arrName(i)
    9.             tempArr(n) = arrName(i): n = n + 1
    10.         End If
    11.     Next
    12.     ReDim Preserve tempArr(n)
    13.     arrName = tempArr
    14. End Sub
    15.  
    16. Private Sub Form_Load()
    17.     Dim x() As String: ReDim x(10)
    18.     x(0) = "a"
    19.     x(1) = "b"
    20.     x(2) = "c"
    21.     x(3) = "d"
    22.     x(4) = "a"
    23.     x(5) = "b"
    24.     x(6) = "c"
    25.     x(7) = "d"
    26.     x(8) = "a"
    27.     x(9) = "b"
    28.     x(10) = "c"
    29.    
    30.     removeDuplicates x
    31.    
    32.     Dim i As Long
    33.     For i = 0 To UBound(x) - 1
    34.         Debug.Print i & ":" & x(i)
    35.     Next
    36. End Sub
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  8. #8
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    ang on... i'll have a bash at the code...
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  9. #9
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    why you need scripting runtime plenderj?
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  10. #10
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    For the dictionary object.
    Its the fastest way (IMHO) to remove duplicates from arrays etc.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Nov 2001
    Location
    Liverpool, England
    Posts
    155
    Hey that's excellent, thanks.

  12. #12
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    well i just wrote this and it worked

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub RemoveDuplicates(ByRef vArray As Variant)
    4. 'removes duplicate items from an array
    5. On Error GoTo ErrorHandler
    6.    
    7.     Dim aTemp() As String
    8.     Dim i As Long
    9.  
    10.     If Not IsArray(vArray) Then Exit Sub
    11.    
    12.     'Go through each item in the array
    13.     For i = LBound(vArray) To UBound(vArray)
    14.         'Only add if not in array already
    15.         If Not InArray(aTemp, vArray(i)) Then
    16.             ReDim Preserve aTemp(UBound(aTemp) + 1)
    17.             aTemp(UBound(aTemp)) = vArray(i)
    18.         End If
    19.     Next i
    20.    
    21.     vArray = aTemp
    22.    
    23.     Erase aTemp
    24.    
    25.     Exit Sub
    26.    
    27. ErrorHandler:
    28.     If Err.Number = 9 Then
    29.         ReDim aTemp(0)
    30.         Resume Next
    31.     Else
    32.         MsgBox Err.Description, , Err.Number
    33.     End If
    34. End Sub
    35.  
    36.  
    37. Private Function InArray(ByVal vArray As Variant, ByVal vItem As Variant) As Boolean
    38. On Error GoTo ErrorHandler
    39.  
    40.     Dim i As Long
    41.    
    42.     If Not IsArray(vArray) Then Exit Function
    43.    
    44.     For i = LBound(vArray) To UBound(vArray)
    45.         If vArray(i) = vItem Then
    46.             InArray = True
    47.             Exit For
    48.         End If
    49.     Next i
    50.  
    51.     Exit Function
    52.  
    53. ErrorHandler:
    54.     If Err.Number = 9 Then
    55.         'nothing - subscript out of range
    56.     Else
    57.         MsgBox Err.Description, , Err.Number
    58.     End If
    59. End Function
    60.  
    61. Private Sub Form_Load()
    62.     Dim aMyArray() As String
    63.     Dim i As Long
    64.    
    65.     ReDim aMyArray(5)
    66.    
    67.     aMyArray(0) = "Bob"
    68.     aMyArray(1) = "Bib"
    69.     aMyArray(2) = "Bab"
    70.     aMyArray(3) = "Bub"
    71.     aMyArray(4) = "Bob"
    72.     aMyArray(5) = "Beb"
    73.    
    74.     RemoveDuplicates aMyArray
    75.    
    76.     For i = LBound(aMyArray) To UBound(aMyArray)
    77.         MsgBox aMyArray(i)
    78.     Next i
    79. End Sub
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  13. #13
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Yeah there's two ways to do it ;

    Use the dictionary object - extremely fast
    Loop back through array for each element of array - extremely slow

    So take your pick
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  14. #14
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    i'm gonna have a look at this dictionary object then, haven't come accross that before
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

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