Solved

Manipulating Arrays

Posted on 1998-11-23
4
212 Views
Last Modified: 2010-05-03
Please could someone offer the shortest code to do the following:

Delete an element in the middle of a dynamic array meaning all the elements above that one in the set are pushed down one.

Test whether a variable equals that of an element in an array.


Thanks
0
Comment
Question by:Jesus112398
[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
4 Comments
 

Expert Comment

by:atelford
ID: 1446408
It would be mouch easier if you used a collection , then you would have no need to resize the array after a delete , but thats just my opinion
0
 
LVL 3

Accepted Solution

by:
CesarGon earned 70 total points
ID: 1446409
For the first problem: the fastest way to do that is to iterate the array from the element deleted up to the last minus one, assigning to each element the value of the next. After that, just ReDim the array to its original length minus one. Example:

  Dim a(1 To 10) As String
  ...

  'let's delete the 4th element.
  Dim l As Long
  For l = 4 To 9
    a(l) = a(l + 1)
  Next l
  ReDim a(1 To 9) As String

For the second problem: iterate the array checking one element at a time.

These are the fastest ways, as measured by me. Nevertheless, collections and/or dictionaries could do better in some cases.

Regards,
César.

0
 
LVL 1

Expert Comment

by:yfang
ID: 1446410
Comments to the proposed answer:
Redim will clear the contents of the array and you will lose the values of all the elements.  You have to use keyword PRESERVE to keep the contents of the elements:

ReDim preserve a(1 To 9) As String

Good luck

0
 
LVL 3

Expert Comment

by:CesarGon
ID: 1446411
Of course you have to use Preserve!. I forgot writing it in my code. Sorry about my mistake and thanks for pointing it out.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

691 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