Results 1 to 7 of 7

Thread: Array Help

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Posts
    126

    Array Help

    I have two arrays both holding people's names, i.e.

    Array 1

    James
    Adam
    Craig
    Bob
    Joe

    Array 2

    James
    Adam
    Susan
    Stephen
    Ruby

    What I want to do is that is search through Array 1 and Array 2 and if the same name appears in
    Array 1 and Array 2 then delete it from Array 1. With the names above you would finish up with:

    Array 1

    Craig
    Bob
    Joe

    Array 2

    James
    Adam
    Susan
    Stephen
    Ruby

    Can someone assist me with this please?

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    This is a place to start, you will need to improve the Array1 reloading!:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4. Dim sArr1(5) As String, sArr2(5) As String
    5. Dim i As Long, x As Long
    6.  
    7.     'Load Arrays
    8.     sArr1(0) = "James"
    9.     sArr1(1) = "Adam"
    10.     sArr1(2) = "Craig"
    11.     sArr1(3) = "Bob"
    12.     sArr1(4) = "Joe"
    13.    
    14.    
    15.     sArr2(0) = "James"
    16.     sArr2(1) = "Adam"
    17.     sArr2(2) = "Susan"
    18.     sArr2(3) = "Stephen"
    19.     sArr2(4) = "Ruby"
    20.  
    21.     'Perform Check, and delete as nesessary
    22.     For i = 0 To UBound(sArr1)
    23.         For x = 0 To UBound(sArr2)
    24.             If LCase(sArr1(i)) = LCase(sArr2(x)) Then sArr1(i) = ""
    25.         Next
    26.     Next
    27.  
    28.     'Display the result
    29.     MsgBox Join(sArr1, vbCrLf)
    30.  
    31. End Sub




    Bruce.

  3. #3
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    I would suggest using collections :

    VB Code:
    1. Private Sub Form_Load()
    2.  
    3. Dim Col1 As New Collection
    4. Dim Col2 As New Collection
    5.  
    6. Col1.Add "James"
    7. Col1.Add "Adam"
    8. Col1.Add "Craig"
    9. Col1.Add "Bob"
    10. Col1.Add "Joe"
    11.  
    12. Col2.Add "James"
    13. Col2.Add "Adam"
    14. Col2.Add "Susan"
    15. Col2.Add "Stephen"
    16. Col2.Add "Ruby"
    17.  
    18. Dim i As Integer
    19. Dim j As Integer
    20.  
    21. On Error Resume Next
    22. For i = 1 To Col1.Count
    23.     For j = 1 To Col2.Count
    24.         If LCase(Col1.Item(i)) = LCase(Col2.Item(j)) Then
    25.             Col1.Remove i
    26.         End If
    27.     Next
    28. Next
    29. On Error GoTo 0
    30.  
    31. For i = 0 To Col1.Count
    32.     MsgBox Col1.Item(i)
    33. Next
    34.  
    35. End Sub


    Has someone helped you? Then you can Rate their helpful post.

  4. #4
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Originally posted by manavo11
    I would suggest using collections :
    Well..... My guess is its an assignment on ARRAYS

  5. #5
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Hm, maybe. The problem (just something annoying) is that it doesn't remove the entries, it makes them equal to "". So you still get an empty line. You avoid it by using collections but anyway...


    Has someone helped you? Then you can Rate their helpful post.

  6. #6
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Originally posted by manavo11
    The problem (just something annoying) is that it doesn't remove the entries, it makes them equal to "". So you still get an empty line.
    Yep, your right, I mentioned that in my post that additional work is required.

    And, valid point re the Collection (and no 'blanks').


    Bruce.

  7. #7
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    I need to start reading more carefully the other posts... Now leave and party


    Has someone helped you? Then you can Rate their helpful post.

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