
Redim an array, using Preserve], or just get it to
<BR>ASP FAQ<BR>Question: How do I dynamically resize an array?<BR>Posted by Scott Mitchell at 9/23/2000 7:53:10 PM<BR><BR>Exactly how does this work?<BR><BR>Redim [Preserve] varname(subscripts)<BR><BR>This is the problem I am running into, I have a nested array<BR><BR><HTML><BODY> <BR><% <BR><BR>x = array(1, 2)<BR>y = array(3, 4)<BR>Cart_Array = array(x, y)<BR>Session("Cart") = Cart_Array<BR><BR>'this above works<BR><BR>z = array(5, 6)<BR><BR>'this is the problem?? how exactly does this work?<BR><BR>Redim [Preserve] Cart_Array(x, y, z)<BR>Session("Cart") = Cart_Array<BR><BR>Temp_Cart = Session("Cart")<BR>x=0<BR>for each item in Temp_Cart<BR> <BR> Temp_Item = Temp_Cart(x)<BR> Item_0 = Temp_Item(0)<BR> Item_1 = Temp_Item(1)<BR> response.write (" Item_0 is <b>" & Item_0 & " </b> Item_1 is <b>" & Item_1 & "</b> <BR>")<BR><BR>x=x+1<BR>next<BR><BR>%> <BR></BODY></HTML> <BR><BR>I appreciate any and all help, thanks again....<BR>Doug<BR>

A few things about redimming and preserving
Ok...I'll clarify some things here...<BR><BR>First<BR><BR>usually, in programming references, words with square brackets means "optional"<BR><BR>So when Scott wrote :<BR><BR>Redim [Preserve] varname(subscripts)<BR><BR><BR>it means in fact<BR><BR>Redim varname(subscripts)<BR>OR<BR>Redim Preserve varname(subscripts)<BR><BR>That's the first thing.<BR><BR>Secondly<BR><BR><BR>x = array(1, 2)<BR>y = array(3, 4)<BR>Cart_Array = array(x, y)<BR>Session("Cart") = Cart_Array<BR><BR>'this above works<BR><BR>z = array(5, 6)<BR><BR>'this is the problem?? how exactly does this work?<BR><BR>Redim [Preserve] Cart_Array(x, y, z)<BR><BR><BR>x is a 2 dimension array<BR>y is a 2 dimension array<BR>z is a 2 dimension array <BR><BR>Cart_Array is a 3 dimension array composed of respectively x, y and z for the three dimensions. (just making sure I understood correctly)<BR><BR>Well... even if you had not put the square brackets in:<BR>Redim [Preserve] Cart_Array(x, y, z)<BR><BR>it still would not have worked<BR><BR>Redim is intended to RESIZE an array NOT REASSIGN its values (don't mean to shout, just want to show the differences)<BR><BR>This means that you line :<BR>Redim [Preserve] Cart_Array(x, y, z)<BR><BR>should actually be something like :<BR>Redim Preserve Cart_Array(UBound(x, 2), UBound(y, 2), UBound(z, 2))<BR><BR>(but again...the above sentence is not very good, BUT it will not crash)<BR><BR>What I think you are trying to do is something like :<BR>Dim Cart_Array(2)<BR><BR>Cart_Array(0) = z<BR>Cart_Array(1) = y<BR>Cart_Array(2) = z<BR><BR>'Then to access the first cell of the z array for instance....<BR><BR>MyTest = Cart_Array(2)(0, 0)<BR><BR><BR>Thirdly<BR><BR>You should know that Redim has its own limitation, its biggest would be that can ONLY resize the LOWER bound of the array<BR><BR>This would work :<BR>Redim Cart_Array(2, UBound(Cart_Array, 2)+1)<BR><BR>This would NOT work:<BR>Redim Cart_Array(UBound(Cart_Array, 1), 4)<BR><BR>If you have a three dimension array then you cannot resize the first two dimensions.<BR><BR><BR>...that's about it.<BR><BR>I hope it will be of some use to you<BR><BR>Eniac

I think 'thirdly' is a bit off
You can redim the holy hell out of an array and its dimensions to your heart's content.<BR><BR>Your statement is only true if the PRESERVE keyword applies. And I know what you meant, but I think instead of using LOWER boung of the array (which makes me think of LBound), it's the LAST dimension of the array (again, only when using Preserve)

thanks for the input...need to clarify, new quesit
First, I will have to read a little more into the threedimensional arrays. But for now, I would like to see if I can make a 2dimensional into a 3.<BR><BR>My guess is:<BR><BR>x = array(1, 2) <BR>y = array(3, 4) <BR>Cart_Array = array(x, y) <BR>Session("Cart") = Cart_Array<BR><BR>x = array(1, 2) <BR>y = array(3, 4) <BR>z = array(5, 6)<BR>Cart_Array = array(x, y, z) <BR>Session("Cart") = Cart_Array <BR> <BR>Is this how you change the array, JUST REASSIGN?<BR><BR>Thanks, for some reason, this did not work, I was just thinking that I had to redim my array. Thanks again for the input<BR>Doug<BR>

Not even close...
...and you got bad stuff in one of the other answers, too.<BR><BR>When you do this:<BR> x = array( 1, 2 )<BR>you are *NOT* creating a two DIMENSIONAL array, at all.<BR><BR>You are simply creating a *ONE*dimensional array that happens to have two ELEMENTS.<BR><BR>In other words, to get an element out that "x" array, you would code:<BR> valueOfSomeElement = x( i )<BR><BR>See that? Only one subscript so it's a ONEdimensional array.<BR><BR>And that *STILL* applies when you do this:<BR> x = array(1, 2) <BR> y = array(3, 4) <BR> z = array(5, 6)<BR> Cart_Array = array(x, y, z) <BR><BR>x is a ONEdimensional array.<BR>y is a ONEdimensional array.<BR>z is a ONEdimensional array.<BR>and, yes, Cart_Array is also is a ONEdimensional array.<BR><BR>It *happens* to be true that each of the *elements* of Cart_Array are in turn a onedimensional array, but that does *NOT* impact what Cart_Array, itself, is.<BR><BR>For example, you *COULD* have coded this:<BR> x = array(1, 2) <BR> y = array(3, 4) <BR> z = array(5, 6)<BR> Cart_Array = array(x, "whatever", y, 3.14159265, z) <BR>and now *elements* 0, 2, 4 are each, in turn an array. But element 1 is a string and element 3 is a double number!<BR><BR>So, finally, when you go to get a value out of Cart_Array, after you do <BR> x = array(1, 2) <BR> y = array(3, 4) <BR> z = array(5, 6)<BR> Cart_Array = array(x, y, z) <BR>you will have to *FIRST* get one of the elements:<BR> temp = Cart_Array( i ) ' temp is a onedimensional array<BR>and *THEN* get one of the elements from the temp array:<BR> singleValue = temp( j )<BR><BR>Now, you *CAN* write a "shorthand" for that:<BR> singleValue = Cart_Array( i )( j )<BR>but that will blow up with an error if Cart_Array(i) is not, itself, an array.<BR><BR>Hokay? <BR><BR>And, by the way, I don't really think this qualifies as an advanced topic, but that's my personal opinion.<BR><BR><BR><BR><BR><BR><BR><BR>

Partly right, but a big error...
x = array(1, 2)<BR>y = array(3, 4)<BR>z = array(5, 6)<BR><BR>x is a 2 dimension array<BR>y is a 2 dimension array<BR>z is a 2 dimension array <BR><BR>****************<BR><BR>NOT TRUE!<BR><BR>x, y, and z are *EACH* just a ONEdimensional array.<BR><BR>See my big long post.<BR><BR>

LOL, yes, got confused there...
Yes, I saw I got confused when DG answered my post.<BR><BR>I read the guy's code too fast and well...wadda know, **** happens...not a very good thing though when you're explaining how array works.<BR><BR>LOL...better for me not to answer too many people here :)<BR><BR>Magic, sorry for the confusion<BR>Bill & DG, thanks for the correction.<BR><BR>Eniac
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

