no****@nospam.com wrote:
I'm fairly new to ASP and must admit its proving a lot more
unnecessarily complicated than the other languages I know. I feel
this is because there aren't many good official resources out there
to help do the most basic things.
One of the "basic" things I haven't been able to find out how to do
is how to delete an item from a multidimensional array object and
resize it afterwards.
It seems so easy to conceive of the code to delete from a
multidimensional array but it just isn't working. Surely it should be
as easy as something like:
myArray.delete(1,0)
myArray.remove(5,7)
In whatever language you are used to using, this must be an easy task. It
sounds as if arrays in that language behave more like what would be called
"collection" in VB. With a collection, items can be removed from or inserted
into the "middle" of the collection. What I am going to talk about applies
to VB, VBA and vbscript, so for simplicity's sake, I am going to refer
simply to "VB" in the following.
With VB arrays, this is not possible. In VB, arrays are "simple" structures
that have no methods for manipulating them such as would be found with a
collection class. VB provides these statements for dealing with arrays
Dim ar([upper index][,...n]): initializes the variable. If an upper index
value is supplied, a fixed-size array is created. If no upper index is
provided, a dynamic array is created (this applies to vbscript - in VB, more
options exist)
ReDim [Preserve]: used to modify an existing array. If used without the
Preserve keyword, all the data in the array is clearedIt can only be used to
change the index of the last dimension in a multi-dimensional array.. In
addition, Preserve will only work when the last dimension in an array is
modified:
dim ar(1,1)
....
redim preserve ar(1,2) ' preserves existing data
redim preserve ar(1,0) 'removes last "row", preserves first "row"
redim preserve ar(2,2) ' clears all existing data (may return error)
Erase: "Reinitializes the elements of fixed-size arrays and deallocates
dynamic-array storage space"
In addition, the following functions exist:
Array(item1,...,itemN): "Returns a Variant containing an array"
LBound(array,[dimension number]): Returns the lowest bound (available
subscript) of the specified array's dimension - not useful in vbscript where
the lowest bound of an array's dimension is always 0
UBound(array,[dimension number]): Returns the upper bound (highest available
subscript) of the specified array's dimension
That's it. This is all you have to work with to manipulate arrays.
So, all you can do is
1. Modify the content of an array element
ar(1,1) = "this"
ar(1,1) = ""
2. Add new elements to the end of a dynamic array:
dim ar()
redim ar(0)
ar(0)="this"
redim preserve ar(1)
ar(1)="that"
Note: ReDim is an "expensive" operation. If you know how many elements will
be needed, you should resize the array in a single statement. Avoid using
redim in a loop:
dim ar()
for i =0 to 3
redim preserve ar(i)
ar(i) = i
next
Instead, do this:
dim ar()
'figure out what the upper bound should be, then
redim ar(3)
for i = 0 to 3
ar(i) = i
next
3. Remove elements from the end of the array (see above)
If you need to be able to remove elements from the middle of an array, you
have to write your own method to do that. Something like:
Sub RemoveAt(ByRef ar, index)
for i = index to ubound(ar) - 1
ar(i) = ar(i+1)
next
redim preserve ar(ubound(ar,1) - 1)
end sub
HTH,
Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.