|
-
Mar 4th, 2002, 05:36 AM
#1
Thread Starter
Addicted Member
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.
-
Mar 4th, 2002, 05:52 AM
#2
Bouncy Member
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:
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
-
Mar 4th, 2002, 06:12 AM
#3
Thread Starter
Addicted Member
Thanks, but how can I work through the elements and delete reoccurring ones, assuming I don't know the contents?
-
Mar 4th, 2002, 06:19 AM
#4
Bouncy Member
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.
-
Mar 4th, 2002, 06:22 AM
#5
Retired VBF Adm1nistrator
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]
-
Mar 4th, 2002, 06:26 AM
#6
Thread Starter
Addicted Member
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
-
Mar 4th, 2002, 06:28 AM
#7
Retired VBF Adm1nistrator
Add a reference to the microsoft scripting runtime library (Project > References), then use this code
VB Code:
Option Explicit
Private Sub removeDuplicates(ByRef arrName() As String)
Dim i As Long, tempArr() As String: ReDim tempArr(UBound(arrName))
Dim d As New Dictionary, n As Long
For i = 0 To UBound(arrName)
If Not d.Exists(arrName(i)) Then
d.Add arrName(i), arrName(i)
tempArr(n) = arrName(i): n = n + 1
End If
Next
ReDim Preserve tempArr(n)
arrName = tempArr
End Sub
Private Sub Form_Load()
Dim x() As String: ReDim x(10)
x(0) = "a"
x(1) = "b"
x(2) = "c"
x(3) = "d"
x(4) = "a"
x(5) = "b"
x(6) = "c"
x(7) = "d"
x(8) = "a"
x(9) = "b"
x(10) = "c"
removeDuplicates x
Dim i As Long
For i = 0 To UBound(x) - 1
Debug.Print i & ":" & x(i)
Next
End Sub
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Mar 4th, 2002, 06:30 AM
#8
Bouncy Member
ang on... i'll have a bash at the code...
-
Mar 4th, 2002, 06:38 AM
#9
Bouncy Member
why you need scripting runtime plenderj?
-
Mar 4th, 2002, 06:40 AM
#10
Retired VBF Adm1nistrator
For the dictionary object.
Its the fastest way (IMHO) to remove duplicates from arrays etc.
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Mar 4th, 2002, 06:44 AM
#11
Thread Starter
Addicted Member
Hey that's excellent, thanks.
-
Mar 4th, 2002, 06:46 AM
#12
Bouncy Member
well i just wrote this and it worked 
VB Code:
Option Explicit
Private Sub RemoveDuplicates(ByRef vArray As Variant)
'removes duplicate items from an array
On Error GoTo ErrorHandler
Dim aTemp() As String
Dim i As Long
If Not IsArray(vArray) Then Exit Sub
'Go through each item in the array
For i = LBound(vArray) To UBound(vArray)
'Only add if not in array already
If Not InArray(aTemp, vArray(i)) Then
ReDim Preserve aTemp(UBound(aTemp) + 1)
aTemp(UBound(aTemp)) = vArray(i)
End If
Next i
vArray = aTemp
Erase aTemp
Exit Sub
ErrorHandler:
If Err.Number = 9 Then
ReDim aTemp(0)
Resume Next
Else
MsgBox Err.Description, , Err.Number
End If
End Sub
Private Function InArray(ByVal vArray As Variant, ByVal vItem As Variant) As Boolean
On Error GoTo ErrorHandler
Dim i As Long
If Not IsArray(vArray) Then Exit Function
For i = LBound(vArray) To UBound(vArray)
If vArray(i) = vItem Then
InArray = True
Exit For
End If
Next i
Exit Function
ErrorHandler:
If Err.Number = 9 Then
'nothing - subscript out of range
Else
MsgBox Err.Description, , Err.Number
End If
End Function
Private Sub Form_Load()
Dim aMyArray() As String
Dim i As Long
ReDim aMyArray(5)
aMyArray(0) = "Bob"
aMyArray(1) = "Bib"
aMyArray(2) = "Bab"
aMyArray(3) = "Bub"
aMyArray(4) = "Bob"
aMyArray(5) = "Beb"
RemoveDuplicates aMyArray
For i = LBound(aMyArray) To UBound(aMyArray)
MsgBox aMyArray(i)
Next i
End Sub
-
Mar 4th, 2002, 06:50 AM
#13
Retired VBF Adm1nistrator
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]
-
Mar 4th, 2002, 08:12 AM
#14
Bouncy Member
i'm gonna have a look at this dictionary object then, haven't come accross that before
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|