Confused about 2D arrays

# Thread: Confused about 2D arrays

1. Senior Member
Join Date
Dec 1969
Posts
11,334

## Confused about 2D arrays

Allrighty..... per Bill&#039;s post http://www.aspmessageboard.com/forum/Advanced.asp?M=182467&F=27&P=1<BR><BR>he&#039;s made this statement:<BR><BR>******************************** ******************************<BR>Then in memory you would find all the elements, contiguously, in this order: <BR><BR>ar(0,0) -- ar(1,0) -- ar(0,1) -- ar(1,1) -- ar(0,2) -- ar(1,2) -- ar(0,3) -- ar(1,3) <BR><BR>So to find element ar(x,y), you can treat this as a single dimensioned array and find the element numbered as <BR> y * ( UBOUND(ar,1) + 1 ) + x <BR><BR>See it? ar(1,2) --&#062;&#062; 2 * ( UBound(ar,1)+1 ) + 1 --&#062;&#062; 2 * 2 + 1 --&#062;&#062; element number 5 [starting at 0, of course]. <BR><BR>If you go count to element number 5 in the list above, you will see that it is, indeed, ar(1,2). <BR><BR>****************************************** ****************<BR><BR>Ok... so if I were to populate this one element at a time (in other words, I don&#039;t have access to ar(1,2)... I need to do it by incrementing a pointer up in a loop). How do you "mesh" up the way VBS sees it? In other words, if I do<BR>for loop<BR>x += "a"<BR>y += "b"<BR>somenumber ++<BR>ar(0) = somenumber<BR>//increment<BR>ar(0) = x &#039;I&#039;ve even tried different variations, such as ar(1)<BR>//increment<BR>ar(0) = y<BR>//increment<BR>end loop<BR><BR>so, this should output, in order 1, a, b, 2, aa, bb.<BR><BR>BUT<BR><BR>In ASP, I get (well, it WILL be in ASP... so ignore the msgbox)<BR><BR> MsgBox "array(0,0) " & z(0,0) &#039;outputs 1<BR> MsgBox "array(0,1) " & z(0,1) &#039;outputs b<BR> MsgBox "array(0,2) " & z(0,2) &#039;outputs aa<BR> MsgBox "array(1,0) " & z(1,0) &#039;outputs a<BR> MsgBox "array(1,1) " & z(1,1) &#039;outputs 2<BR> MsgBox "array(1,2) " & z(1,2) &#039;outputs bb<BR><BR><BR><BR>I&#039;ve tried a few variations, but I can&#039;t seem to line up the array elements.... what am I missing here?<BR><BR>

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

## I wrote that?

Heh. Just kidding.<BR><BR>Let&#039;s see...so you want to treat a VBS 2D array as a 1D array in C++ (or in JS, I guess?).<BR><BR>Rather than using a pointer, I&#039;d do it this way:<BR><BR>for ( row = 0; row &#060; rowcount; ++row )<BR>{<BR> for ( col = 0; col &#060; colcount; ++col )<BR> {<BR> ar[ row * rowcount + col ] = "whatever";<BR> }<BR>}<BR><BR>Ummm...but I guess that&#039;s what I said in the article.<BR> y * ( UBOUND(ar,1) + 1 ) + x<BR><BR>Ummm...I dunno.<BR><BR>What language are you doing this in, as if it matters?<BR><BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
11,334

## Heh

It&#039;s a C++ SAFEARRAY.<BR><BR>I&#039;ve been killing myself trying to reverse the formula you had in there to no avail today.<BR><BR>I&#039;ve seen the examples out there that do the dual for loop, but I can&#039;t figure out how to change the "whatever" into something that I need.... 3 days now, trying with this stupid 2D array, it&#039;s confusing me more than I ever have been in my life (well, programming wise at least).<BR><BR>So doing the for loop, how do you get the "whatever" into whatever you want (obviously having the data change to be "proper")

4. Senior Member
Join Date
Dec 1969
Posts
11,334

## Hmmmm... still errors

If I do this (which should be the same basis of what you&#039;re showing me)<BR><BR> for (r = 0; r &#060; 2; r++)<BR> {<BR> x += "a";<BR> y += "b";<BR> strs[r + 0].intVal = ++uid;<BR> strs[r + 1].bstrVal = A2BSTR(x.c_str());<BR> strs[r + 2].bstrVal = A2BSTR(y.c_str());<BR> }<BR><BR>THAT should&#039;ve done it... but now I do know the element "0" will have to be element "3" on the way back, but it doesn&#039;t seem to mesh.<BR><BR>Am I missing something vital here?

5. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

## OT: i like the variable "names" you use <eop&

.

6. Senior Member
Join Date
Dec 1969
Posts
11,334

## These are tests until I get the stupid thing to wo

I had "regular" variables in there, but it wasn&#039;t working, so test with silly data, I just use quickies. C&#039;mon :p<BR><BR>If it makes you feel any better, it&#039;s actually<BR><BR> strs[z + r + 2].vt = VT_BSTR;<BR> strs[z + r + 2].bstrVal = A2BSTR(event-&#062;eventName.c_str());<BR><BR>strs to me means "strings" which it kinda is (It&#039;s actually a variant array of subtype string, but hey.... *I* know what it means ;)

7. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

## the z and the r are the cool ones <eop>

.

8. Senior Member
Join Date
Dec 1969
Posts
11,334

## Again, temporary until it works

I do that all the time... though the z and the r will combine to be i, so how&#039;s that?

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

## That makes no sense at all!

Effectively, you are doing:<BR><BR>// for loop: r=0<BR>strs[0].intVal = ++uid;<BR>strs[1].bstrVal = A2BSTR(x.c_str());<BR>strs[2].bstrVal = A2BSTR(y.c_str());<BR>// for loop: r=1<BR>strs[1].intVal = ++uid;<BR>strs[2].bstrVal = A2BSTR(x.c_str());<BR>strs[3].bstrVal = A2BSTR(y.c_str());<BR>// for loop: r=2<BR>strs[2].intVal = ++uid;<BR>strs[3].bstrVal = A2BSTR(x.c_str());<BR>strs[4].bstrVal = A2BSTR(y.c_str());<BR> <BR>And you never set ANY values into anything beyond strs[4], do you?<BR><BR>You should be doing:<BR><BR>strs[r*3 + 0].intVal = ++uid;<BR>strs[r*3 + 1].bstrVal = A2BSTR(x.c_str());<BR>strs[r*3 + 2].bstrVal = A2BSTR(y.c_str());<BR><BR>Or something along those lines.<BR><BR>

10. Senior Member
Join Date
Dec 1969
Posts
11,334

## Yeah, I did change

To basically the same thing... the d + r + 0. (and increment d by 2 at the end)<BR><BR>Same results as before... <BR><BR> MsgBox "array(0,0) " & z(0,0) &#039;outputs 1<BR> MsgBox "array(0,1) " & z(0,1) &#039;outputs b<BR> MsgBox "array(0,2) " & z(0,2) &#039;outputs aa<BR> MsgBox "array(1,0) " & z(1,0) &#039;outputs a<BR> MsgBox "array(1,1) " & z(1,1) &#039;outputs 2<BR> MsgBox "array(1,2) " & z(1,2) &#039;outputs bb

#### Posting Permissions

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