Delete ASP Multidimensional Array

G

Guest

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)

but what it is and how to find out - there just doesn't seem to be a way. I
reckon there must have been some collusion going on between m$ and the
writers of books called things like "Learn ASP in 4 days!!!" to stifle the
spread of information in order to make money because this language is
unreal!
 
S

Slim

try
redim yourArray(5,5)

if you want to preserver the existing data

redim preserve yourArray(6,6)

this is all listed in the vbscript file
 
E

Evertjan.

Slim wrote on 23 mrt 2006 in microsoft.public.inetserver.asp.general:
try
redim yourArray(5,5)

if you want to preserver the existing data

redim preserve yourArray(6,6)

this is all listed in the vbscript file

The problem is, that ASP is not a language at all, but a platform for
serverside scripting languages, like vbscript and jscript.

It seems that you are both ignoring that and consequently don't connect.
 
A

Anthony Jones

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)

but what it is and how to find out - there just doesn't seem to be a way. I
reckon there must have been some collusion going on between m$ and the
writers of books called things like "Learn ASP in 4 days!!!" to stifle the
spread of information in order to make money because this language is
unreal!

ASP is essentially a scripting host with some ASP specific objects loaded
into the scripting context.

To use ASP you really need to look for documentation related not only to ASP
but also to the script language you are using.

Take look at these for documentation of the ASP objects

http://msdn.microsoft.com/library/en-us/iissdk/html/3e31cf7f-8145-4a3e-8902-11d97c2a08c2.asp

http://msdn.microsoft.com/library/en-us/iissdk/html/2c40c3cf-90eb-41ca-ae2a-0ef33a651779.asp

Then take look at this if you are using Javascript:-

http://msdn.microsoft.com/library/en-us/script56/html/1e9b3876-3d38-4fd8-8596-1bbfe2330aa9.asp

Or this if VBScript:-

http://msdn.microsoft.com/library/en-us/script56/html/0a8270d7-7d8f-4368-b2a7-065acb52fc54.asp

Given the choice I prefer Javascript but if you want to gather and use ASP
examples from around the web you might want to stick with VBScript.

Anthony.
 
M

Mike Brind

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.

Mistake number one - ASP is not a language. It's a technology. You
can access this technology using a number of different languages,
including JScript, Javascript, Perl and VBScript - the latter seems to
be the one used by the vast majority of developers.

When you search for code and help, I suggest you add the language you
are using for your scripts eg "VBScript arrays", and you shouldn't have
any problem getting help.

You might also like to download the language reference for VBScript
from Microsoft (it's free, by the way) here:
http://www.microsoft.com/downloads/...43-7e4b-4622-86eb-95a22b832caa&DisplayLang=en

and then visit the official ASP documentation here:
http://msdn.microsoft.com/library/d...html/2c40c3cf-90eb-41ca-ae2a-0ef33a651779.asp

Good luck
 
G

Guest

try
redim yourArray(5,5)

<%
Dim mycartArray
Redim mycartarray(1,1)
mycartArray(0,0) = "this"
mycartArray(0,1) = "that"
mycartArray(1,0) = "them"
mycartArray(1,1) = "those"
redim mycartArray(1,1)

Response.Write "<table><tr>"
Response.Write "<td bgcolor=#dddddd>00" & mycartArray(0,0) & "</td></tr>"
Response.Write "<td bgcolor=#dddddd>01" & mycartArray(0,1) & "</td></tr>"
Response.Write "<td bgcolor=#dddddd>10" & mycartArray(1,0) & "</td></tr>"
Response.Write "<td bgcolor=#dddddd>11" & mycartArray(1,1) & "</td></tr>"
Response.Write "</table>"
%>

Result:
00
01
10
11
if you want to preserver the existing data

redim preserve yourArray(6,6)

<%
Dim mycartArray
Redim mycartarray(1,1)
mycartArray(0,0) = "this"
mycartArray(0,1) = "that"
mycartArray(1,0) = "them"
mycartArray(1,1) = "those"
redim preserve mycartArray(1,1)

Response.Write "<table><tr>"
Response.Write "<td bgcolor=#dddddd>00" & mycartArray(0,0) & "</td></tr>"
Response.Write "<td bgcolor=#dddddd>01" & mycartArray(0,1) & "</td></tr>"
Response.Write "<td bgcolor=#dddddd>10" & mycartArray(1,0) & "</td></tr>"
Response.Write "<td bgcolor=#dddddd>11" & mycartArray(1,1) & "</td></tr>"
Response.Write "</table>"
%>

Result:
00this
01that
10them
11those

I'm sorry to be thick but how is this deleting or preserving what I need it
to? I want to clear item (1,1) of the value "those" and retain all the other
information in the array.
 
G

Guest

You might also like to download the language reference for VBScript

Arrgh! I'm really not having any luck here. I followed your link and it was
just the lastest scripting installation package - no docs or help concerning
where it may or may not have installed any docs. But I found a link from
that page which was for the documentation I think you mean to link me to and
then when I downloaded that it looked excellent but the first page I was
greeted with was page cannot be displayed and none of the other references
go anywhere or do anything (and I took my firewall off temporarily to work
out why its not loading).

Thanks - I'm gonna need it..
 
S

Slim

<%
Dim mycartArray
Redim mycartarray(1,1)
mycartArray(0,0) = "this"
mycartArray(0,1) = "that"
mycartArray(1,0) = "them"
mycartArray(1,1) = "those"
redim mycartArray(1,1)

Response.Write "<table><tr>"
Response.Write "<td bgcolor=#dddddd>00" & mycartArray(0,0) & "</td></tr>"
Response.Write "<td bgcolor=#dddddd>01" & mycartArray(0,1) & "</td></tr>"
Response.Write "<td bgcolor=#dddddd>10" & mycartArray(1,0) & "</td></tr>"
Response.Write "<td bgcolor=#dddddd>11" & mycartArray(1,1) & "</td></tr>"
Response.Write "</table>"
%>

Result:
00
01
10
11


<%
Dim mycartArray
Redim mycartarray(1,1)
mycartArray(0,0) = "this"
mycartArray(0,1) = "that"
mycartArray(1,0) = "them"
mycartArray(1,1) = "those"
redim preserve mycartArray(1,1)

Response.Write "<table><tr>"
Response.Write "<td bgcolor=#dddddd>00" & mycartArray(0,0) & "</td></tr>"
Response.Write "<td bgcolor=#dddddd>01" & mycartArray(0,1) & "</td></tr>"
Response.Write "<td bgcolor=#dddddd>10" & mycartArray(1,0) & "</td></tr>"
Response.Write "<td bgcolor=#dddddd>11" & mycartArray(1,1) & "</td></tr>"
Response.Write "</table>"
%>

Result:
00this
01that
10them
11those

I'm sorry to be thick but how is this deleting or preserving what I need
it to? I want to clear item (1,1) of the value "those" and retain all the
other information in the array.


ok
mycartArray(1,1) = ""
 
B

Bob Barrows [MVP]

Arrgh! I'm really not having any luck here. I followed your link and
it was just the lastest scripting installation package - no docs or help
concerning where it may or may not have installed any docs. But I found a
link
from that page which was for the documentation I think you mean to link me
to and

Was it this link?
http://www.microsoft.com/downloads/...48-207D-4BE1-8A76-1C4099D7BBB9&displaylang=en
then when I downloaded that it looked excellent but the first page I
was greeted with was page cannot be displayed and none of the other
references

Where did you install it? I've seen that in certain cases where I didn't
install the chm file to my local hard drive.

You can read the vbscript docs online at
http://msdn.microsoft.com/library/en-us/script56/html/0a8270d7-7d8f-4368-b2a7-065acb52fc54.asp

This will be the same documentation that is included in the download in the
previous link.
In addition, you will find documentation for all MS developer technologies
at this website.

Bob Barrows
 
A

Anthony Jones

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.

Using any syntax of any language with which you are currently familiar can
you post the code that would do this "basic" thing?

It's fairly easy to set the contents of an array element to a null or empty
value but I suspect what you really want to do is destroy the 'cell'
completely, moving all other cells up some how. You haven't specified how
exactly though.

For example in a 3x3 matrix if item(0,1) is destroyed does item(1,0) become
item(0,2)?

Anthony.
 
B

Bob Barrows [MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top