Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

An array list....?

Status
Not open for further replies.

damann

Technical User
Jul 19, 2000
114
0
0
US
Ok,

I have a list of things in an array, they are all dynamic. How would you guys say is the easiest way to add a new element to the middle of this array after which having the elements above incrementing?

For example, here is my list:

myArray(0) = Money
myArray(1) = Cars
myArray(2) = Girls

Now, let's say I want to shove the word 'Mansions' before Girls, how would I go about doing so? If not by using arrays, what would be a better way of accomplishing this?

Thanks in advance...
Damann

 
this might work,,,haven't tested it though,,
y=5
newword=newword

for x = y to ubound(myarray)
oldword = myarray(x+1)
myarray(x)=newword
newword = oldword
next
redim preserve myarray(x+1)
myarray(x+1)=newword

don't know of any easy way,

good luck

tsmith
 
There is no "One Line" of code to do this in VBScript. You'll have to ReDim your array then loop through the array backwards moving each element to the next higher location in the array. When you reach the location you want to put your data stop and write that data.

i.e.
sub InsertElement (intLocation, strData)
dim intNumElements
dim intCount

intNumElements = UBound(MyArray) + 1
ReDim MyArray(intNumElements)

for intCount = intNumElements to intLocation step - 1
MyArray(intCount) = MyArray(intCount -1)
next

MyArray(intLocation) = strData
end sub

Something like that should work. Hope that helps.

Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top