Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Syntax for accessing 2 dimensional array? 1

Status
Not open for further replies.

FancyPrairie

Programmer
Oct 16, 2001
2,917
0
0
US
I have a query: "Select a, b from table1" which I open using ado. I then dump the contents of the recordset to an array using GetRows. Looks something like this:
Code:
blah, blah, blah...
rst.open "Select"....
MyArray = rst.GetRows
My question is, how to I reference the various dimensions of MyArray. I have tried about every combination (i.e. MyArray[0][0], etc) but no luck. In Debug I can see the structure and values. Looks something like this:
Code:
- MyArray
     + (0)
     + (1)

If I expand (0), it looks like this in debug:

- MyArray
     - (0)
           (0)  value0
           (1)  value1
           (2)  value2
           (n)  valuen
     + (1)

So, what's the syntax for retrieving "value0"?
 
'MyArray[0][0]' should work for a 2D array:

Code:
var MyArray = [[1, 2], [3, 4], [5, 6]];
alert(MyArray[0][0]); // Should show 1

So, perhaps that's not the problem?

Your code above doesn't look like JS, so I'm assuming you're using VBScript/ASP to do the SQL command. So, how are you creating and populating the JS array from your SQL recordset?

Are you even populating a JS array?

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
MyArray is passed back as vbarray object. Its entries can be accessed like this for inspection.
[tt]
var s;
for (var j=0;j<=MyArray.ubound(2);j++) {
s="";
for (var i=0;i<=MyArray.ubound(1);i++) {
s+=MyArray.getItem(i,j)+",";
}
s=s.substr(s,s.length-1);
//response.write(s+"<br />"); //server-side
//alert(s); //client-side
}
[/tt]
 
This is a typo!
>[self]s=s.substr(s,s.length-1);
[tt]s=s.substr([red]0[/red],s.length-1);[/tt]
 
If you're uncomfortable using the VBArray object, it supports a method called [!]toArray()[/!], but take note that this will convert your 2 dimensional array into a single dimension array. So, you will have to keep track of how many columns were returned from your query so you'll know when each new "row" begins.

I rarely use this method, but it can be easier for someone not accustomed to VBScript.

-kaht

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top