Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Manipulating Arrays

Posted on 1998-11-23
4
Medium Priority
?
222 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 140 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

636 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