Results 1 to 9 of 9

Thread: Removing a specific string from an array, and redimming?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2003
    Posts
    1,269

    Removing a specific string from an array, and redimming?

    How can you remove a specific string from an array (totally), and redim it so that the blank doesnt remain?

    IE> In an array (3)

    Hello
    Test
    Help

    and i want to remove "Test".. to make the new array(2) look like this:

    Hello
    Help


    anyone know?

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    While it can be done with an array, it's easier to use a collection.

    VB Code:
    1. Dim MyCollection As New Collection
    2.     Dim intIndex As Integer
    3.    
    4.     ' Create the test data
    5.     MyCollection.Add "Hello", "Hello" ' 1st is Item, 2nd is key. Keys must be unique
    6.     MyCollection.Add "Test", "Test"
    7.     MyCollection.Add "Help", "Help"
    8.    
    9.    
    10.     ' Remove one of them
    11.     MyCollection.Remove "Test"
    12.    
    13.    
    14.     ' Show the results
    15.     For intIndex = 1 To MyCollection.Count
    16.         Debug.Print MyCollection(intIndex)
    17.     Next

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2003
    Posts
    1,269
    arent collections a lot slower than arrays though?

  4. #4

  5. #5
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762
    how about:

    if a() contains your array of strings
    Code:
     
    for i = lbound(a) to ubound(a)
    if a(i) <> YourSearchString then
    b(j) = a(i)
    j=j+1
    next i
    redim a(j)
    a=b
    just a thought
    kevin

    ps I haven't tested it
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2003
    Posts
    1,269
    the only drawback to the collection is that unique key..
    i wont be adding all unique items to the array tho
    how can i get around that?

  7. #7
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    You don't need to inclue a key. It is an optional thing. Just don't enter anything there.
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  8. #8
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    In that case

    VB Code:
    1. Dim intIndex As Integer
    2.     Dim MyArray() As String
    3.     Dim intTwo As Integer
    4.     Dim bDeleted As Boolean
    5.    
    6.     ReDim MyArray(4)
    7.     MyArray(0) = "Hello"
    8.     MyArray(1) = "Test"
    9.     MyArray(2) = "Help"
    10.     MyArray(3) = "one"
    11.     MyArray(4) = "Test"
    12.    
    13.     bDeleted = True
    14.     Do Until bDeleted = False
    15.         bDeleted = False
    16.         For intIndex = 0 To UBound(MyArray)
    17.             If MyArray(intIndex) = "Test" Then
    18.                 For intTwo = intIndex To UBound(MyArray) - 1
    19.                     MyArray(intTwo) = MyArray(intTwo + 1)
    20.                     bDeleted = True
    21.                 Next
    22.                 ReDim Preserve MyArray(UBound(MyArray) - 1)
    23.             End If
    24.             If bDeleted Then
    25.                 Exit For
    26.             End If
    27.         Next
    28.     Loop

  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Originally posted by BuggyProgrammer
    You don't need to inclue a key. It is an optional thing. Just don't enter anything there.
    But then you can't remove an item without looping through the collection, and the ability to "grab" the item in this case is the advantage of the collection.

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