Multidimensional Arrays (arghh!)

Thread: Multidimensional Arrays (arghh!)

1. Junior Member
Join Date
Dec 1969
Posts
3

Multidimensional Arrays (arghh!)

Data: (User, Gift#)<BR>1,7<BR>1,23<BR>1,44<BR>2,9<BR>3,14<BR>3, 22<BR>I get this into an two dimensional array as follows:<BR>arTmp(0,0)=1<BR>arTmp(1,0)=7<BR>arTmp( 0,0)=1<BR>arTmp(1,0)=23<BR>arTmp(0,0)=1<BR>arTmp(1 ,0)=44<BR>arTmp(0,0)=2<BR>arTmp(1,0)=9<BR>arTmp(0, 0)=3<BR>arTmp(1,0)=14<BR>arTmp(0,0)=3<BR>arTmp(1,0 )=22<BR>My problem is that when cycling thru the array I can&#039;t get the right values:<BR>for i=0 to UBound(arTmp,2) &#039;Users<BR> for j=0 to UBound(arTmp,2) &#039;Gift#<BR> response.write(arTmp(i,j))<BR> next<BR>next<BR>The result of this is reporting values Gift#&#039;s for a User that don&#039;t exist!<BR><BR>Help!

2. Junior Member
Join Date
Dec 1969
Posts
3

RE: Multidimensional Arrays (arghh!)

Sorry I posted to fast.<BR>the "j" loop should be to UBound(arTmp,1)

3. Senior Member
Join Date
Dec 1969
Posts
96,118

Well, that's pretty darned bogus

arTmp(0,0)=1<BR>arTmp(1,0)=7<BR>arTmp(0,0)=1 -- so this replaces the prior arTmp(0,0)<BR>arTmp(1,0)=23 -- so this replaces the prior arTmp(1,0)<BR>arTmp(0,0)=1 -- so this replaces the prior arTmp(0,0)<BR>arTmp(1,0)=44 -- so this replaces the prior arTmp(1,0)<BR>arTmp(0,0)=2 -- so this replaces the prior arTmp(0,0)<BR>arTmp(1,0)=9 -- so this replaces the prior arTmp(1,0)<BR>arTmp(0,0)=3 -- so this replaces the prior arTmp(0,0)<BR>arTmp(1,0)=14 -- so this replaces the prior arTmp(1,0)<BR>arTmp(0,0)=3 -- so this replaces the prior arTmp(0,0)<BR>arTmp(1,0)=22 -- so this replaces the prior arTmp(1,0)<BR><BR>So you end up with only the elements (0,0) and (1,0) in your array. That was kind of a waste of time, wasn&#039;t it?<BR><BR>You know, you&#039;d be tons better off *NOT* using a 2D array.<BR><BR>Instead, use an Array-of-arrays:<BR><BR>&#060;%<BR>arTmp = Array( _<BR> Array(1,7), _<BR> Array(1,23), _<BR> Array(1,44), _<BR> Array(2,9), _<BR> Array(3,14), _<BR> Array(3,22) _<BR> )<BR><BR>And now you can access the elements via<BR> arTmp( row )( col )<BR><BR>So:<BR><BR>For row = 0 To UBound(arTmp)<BR> For col = 0 to 1<BR> Response.Write arTmp(row)(col)<BR> Next<BR>Next<BR><BR><BR>

4. Junior Member
Join Date
Dec 1969
Posts
3

RE: Well, that's pretty darned bogus

Thx Bill... even tho&#039; I laid it out wrong. I was doing a redim preserve and incrementing the 2nd dimension.<BR><BR>arTmp(0,0)=1<BR>arTmp(1,0)=7<BR> arTmp(0,1)=1<BR>arTmp(1,1)=23<BR>arTmp(0,2)=1<BR>a rTmp(1,2)=44<BR>arTmp(0,3)=2<BR>arTmp(1,3)=9<BR>ar Tmp(0,4)=3<BR>arTmp(1,4)=14<BR>arTmp(0,5)=3<BR>arT mp(1,5)=22<BR><BR>I like your way better.

5. Senior Member
Join Date
Dec 1969
Posts
96,118

Yes, another thing to avoid...

ReDim Preserve is one of the most expensive things you can do in VBScript. Ranks right up there with REPLACE in terms of slow performance.<BR><BR>

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•