Solved

removing items from an array

Posted on 2004-09-03
3
218 Views
Last Modified: 2010-05-02
How do I do the following:

I have a dynamic array: ItemList() and it contains the following 7 items of data:

Bob
Anne
Jane
Mary
Tim
Jack
John

along the way, items are tagged as no longer availible and I've replaced them with [REMOVED] so my list of 7 items now reads:

Bob
Anne
[REMOVED]
Mary
Tim
[REMOVED]
John

whats the best way to take away the [REMOVED] items so i am left with an array of 5 items:

Bob
Anne
Mary
Tim
John

Cheers,

`Andy
0
Comment
Question by:xizor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
JR2003 earned 125 total points
ID: 11972932
This should do the trick:

Private Sub Command1_Click()
    Dim MyArray() As String
    ReDim MyArray(1 To 5)
    MyArray(1) = "qwe"
    MyArray(2) = "[REMOVED]"
    MyArray(3) = "qwe"
    MyArray(4) = "[REMOVED]"
    MyArray(5) = "qwe"
    Dim i As Long
    Dim j As Long
    j = 0 'Initialise
    For i = LBound(MyArray) To UBound(MyArray)
        If (i + j) > UBound(MyArray) Then Exit For
        If MyArray(i + j) = "[REMOVED]" Then
            j = j + 1
        End If
        If j <> 0 And (i + j) <= UBound(MyArray) Then
            MyArray(i) = MyArray(i + j)
        End If
    Next i
    If j <> 0 Then
        ReDim Preserve MyArray(LBound(MyArray) To UBound(MyArray) - j)
    End If
End Sub
0
 
LVL 2

Author Comment

by:xizor
ID: 11973073
Thanks!
0
 
LVL 18

Expert Comment

by:JR2003
ID: 11973456
Ooops, I just realised there is a bug in the code I just geve you.
This version works
JR

Private Sub Command1_Click()
    Dim MyArray() As String
    ReDim MyArray(1 To 6)
    MyArray(1) = "[REMOVED]"
    MyArray(2) = "[REMOVED]"
    MyArray(3) = "[REMOVED]"
    MyArray(4) = "QWE4"
    MyArray(5) = "[REMOVED]"
    MyArray(6) = "[REMOVED]"
    Dim i As Long
    Dim j As Long
    j = 0 'Initialise
    For i = LBound(MyArray) To UBound(MyArray)
        If i + j > UBound(MyArray) Then Exit For
        MyArray(i) = MyArray(i + j)
        While (MyArray(i) = "[REMOVED]") And (i + j) < UBound(MyArray)
            j = j + 1
            MyArray(i) = MyArray(i + j)
        Wend
    Next i
    If j <> 0 Then
        ReDim Preserve MyArray(LBound(MyArray) To UBound(MyArray) - j - LBound(MyArray))
    End If
    For i = LBound(MyArray) To UBound(MyArray)
        Debug.Print MyArray(i)
    Next i
End Sub

0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month5 days, 21 hours left to enroll

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question