Multi dimensional arrays and recursion

Thread: Multi dimensional arrays and recursion

1. Senior Member
Join Date
Dec 1969
Posts
115

Multi dimensional arrays and recursion

so I was looking at popping and pushing multidimensional arrays in vbscript, (I know, I&#039;m a glutton for punishment), please let me know if I&#039;m on the right track.<BR><BR>You can address a 2D array by looking for:<BR><BR>myarray(4,7)<BR><BR>and you can get the size of each dimension by doing:<BR>columns=ubound(myarray,1)<BR>rows=ubound (myarray,2)<BR><BR>but what happens when I go with 3 dimensions?<BR>if I try:<BR>myarray(0,0,0)<BR>I get an out of range error.<BR><BR>Why doesn&#039;t this syntax extend to higher dimensions. I can see the array I&#039;ve created in MDE so how do I address high dimensions of a multi dimensional array?<BR><BR>The end goal here is to be able to recurse through an array and select, update and remove items of my choosing. Has anyone done this sort of thing?

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

??? One step at a time...

(1) Where did you *create* the 3D array???<BR><BR>(2) What is "MDE"???<BR><BR>(3) Arrays are *TERRIBLE* things to use when you want to "remove items". Especially VBS multi-dimensional arrays. Ugly and slow code. Better to simply find a way to mark a row as deleted and skip it on subsequent searches.<BR><BR>(4) Can you explain what kind of data you would store in a 3D array??? Certainly nothing you would (ordinarily) store in a DB.<BR><BR>(5) Try this:<BR>&#060;%<BR>Dim ar(2,4,3)<BR>For d1 = 0 To UBound(ar,1)<BR> For d2 = 0 To UBound(ar,2)<BR> For d3 = 0 To UBound(ar,3)<BR> ar(d1,d2,d3) = "Element " & d1 & d2 & d3<BR> Next<BR> Next<BR>Next<BR>Response.Write ar(1,4,2) & "&#060;br/&#062;" & ar(2,2,1) & "&#060;P&#062;"<BR>%&#062;

3. Senior Member
Join Date
Dec 1969
Posts
115

RE: ??? One step at a time...

(1) Where did you *create* the 3D array???<BR>in MDE<BR>(2) What is "MDE"???<BR>Microsoft Development Environment<BR><BR>I use it to debug my vbscript<BR><BR>(3) Arrays are *TERRIBLE* things to use when you want to "remove items". Especially VBS multi-dimensional arrays. Ugly and slow code. Better to simply find a way to mark a row as deleted and skip it on subsequent searches. <BR><BR>I agree, but what you suggest doesn&#039;t sound like it&#039;s scalable to the problem I have to solve, which is removing array A from array B and placing it in Array C regardless of how many dimensional A B or C have. I suppose I could try a more complicated query to the DB, but doing recursion on multidimensional arrays seemed more versatile.<BR><BR>(4) Can you explain what kind of data you would store in a 3D array??? Certainly nothing you would (ordinarily) store in a DB.<BR>any time you join two tables, you can view that as a multidimensional array. IF you have arrays L, W, D where each item in D is referenced by it&#039;s first element being a part of W which is referenced by it&#039;s first element being a part of L... It&#039;s just another way of handling disparate data.<BR>If I make a call to the database and get back 3 rows with 5 fields, that&#039;s one array, If I do the query again, but altered to get slightly different data, that would be another, if I want to move these around easily, I put them both in an array, bang, 3D array... If there&#039;s an easier way, I&#039;m all ears...<BR><BR>(5) Try this:<BR>I did and I got<BR>Element 221<BR><BR>which I don&#039;t think is correct, but I&#039;m not sure what you were trying to tell me.<BR><BR>I know vbscript sucks for this stuff, but I&#039;m stuck with a legacy I&#039;m not allowed to change.<BR><BR>Thanks for your help

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

I meant WHAT CODE...

...did you use to create the array.<BR><BR>And I&#039;ve never heard of "Microsoft Development Environment". I presume you actually mean either Visual Studio or Visual Interdev?<BR><BR>But regardless...<BR><BR>(1) Where did you *create* the 3D array???<BR><BR>Meant "Where in your code? And please SHOW the code."<BR><BR>***********<BR><BR>If I make a call to the database and get back 3 rows with 5 fields, that&#039;s one array<BR><BR>And *HOW* do you convert the recodset to an array?<BR><BR>If you use ANYTHING other than ADODB.Recordset.GetRows then you have already screwed up big time.<BR><BR>If I do the query again, but altered to get slightly different data, that would be another<BR><BR>Yes, another 2D array, obtained from GETROWS(), I sure would assume.<BR><BR>BUT...<BR><BR>But unless you have SELECTed the *same* fields (at least by type, if not by name) in both the queries, that pair of 2D arrays has no logical way of being joined into anything, much less a 3D array.<BR><BR>For example, if the second query gets back 10 rows with 12 fields each, how can you possibly reasonably match that up with the array from the first query?<BR><BR>And then, if the two arrays *DO* consist of the SAME number of columns and same column types, then WHY did you ever use TWO queries in the first place??? If they are that homogeneous, you should have used a UNION query to get all the data with ONE query.<BR><BR>*******<BR><BR> if I want to move these around easily, I put them both in an array, bang, 3D array<BR><BR>But that means you have to *COPY* ALL THE ELEMENTS into the ugly 3D array. Slow slow slow and clumsy.<BR><BR>Instead, you could simply do:<BR>&#060;%<BR>...<BR>Set RS = conn.Execute(FirstQuery)<BR>ar1 = RS.GetRows()<BR>RS.Close<BR>Set RS = conn.Execute(SecondQuery)<BR>ar2 = RS.GetRows()<BR><BR>allRecords = Array( ar1, ar2 )<BR>%&#062;<BR><BR>And now you can index into that via:<BR> allRecords( whichQuery )( column, row )<BR><BR>So now you have an ARRAY *OF* 2D arrays. <BR><BR>Which is effectively a 3D array but without the horrible overhead of copying element by element.<BR><BR>***********<BR><BR>But I say again: I think that the overall plan of attack may need to be reconsidered.<BR><BR>Especially whether or not you should be using a UNION query, for example.<BR><BR>******************<BR><BR>(5) Try this:<BR>I did and I got<BR>Element 221<BR><BR>which I don&#039;t think is correct, but I&#039;m not sure what you were trying to tell me.<BR><BR>Yes, that is correct. (Should have gotten two Element numbers, though, not just the one.)<BR><BR>I was trying to demonstrate that (a) 3D arrays *DO* work, (b) you *CAN* use UBOUND(arrayname,3) to get the 3rd upper bount, (c) the arrays can be "sparse" in the sense that not all elements have to have values.<BR><BR><BR><BR><BR>... If there&#039;s an easier way, I&#039;m all ears...<BR>

5. Senior Member
Join Date
Dec 1969
Posts
115

RE: I meant WHAT CODE...

Ah this is helpful, thanks much.<BR><BR>I&#039;m doing exactly as you suggest for combining the 2D arrays into a 3D array. But when I do that and try to get the ubound, ubound(myarray, 3) gives me an out of range error. I can get it by doing ubound(myarray(0),2) which seems a strange way to get it but I understand the logic. but it doesn&#039;t seem to scale. ubound(myarray(0,0),2) gives an OOR error. So if I want to be able to manipulate multidimensional arrays to an infinite degree, I seem to be stuck.<BR><BR>I&#039;m not very familiar with SQL so I will look at the UNION query and see if that&#039;s a better solution.<BR><BR>One problem I have with SQL queries is I need to make a comparision on part of a string held in the DB. But I haven&#039;t been able to figure out if regular expressions work within a SQL query. a simple "Like" comparison is not specific enough. But I guessed there was probably a better forum to ask this question. Do you have a suggestion as to where to ask SQL questions?

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

I'll be darned!

So you *are* using the form:<BR> myArray = Array( one2DArray, another2DArray ) &#039; and so on<BR><BR>Then you need to realize that myArray is itself just a ONE dimensional array!!!<BR><BR>Each *ELEMENT* of that 1D array is than a 2D array.<BR><BR>So doing<BR> Ubound( myArray(0), 2 )<BR>is just shorthand for doing<BR> oneOfThe2DArrays = myArray(0) <BR> UBound( oneOfThe2DArrays )<BR><BR>And, in fact, if you are going to be working with on of those array at a time, then it would be a *TON* more efficient to indeed copy it OUT of the array-of-arrays first:<BR> current2D = myArray( current2DarrayNumber )<BR> For row = 0 To UBound( current2D, 2 )<BR> ...<BR><BR>And OF COURSE<BR> UBound( myArray(0,0), 2 )<BR>doesn&#039;t work. There is NO SUCH THING as <BR> myArray(0,0)<BR>We just said that myArray is a ONE-dimensional array. It is not and never will be a 2D array.<BR><BR>****************<BR><BR>I haven&#039;t been able to figure out if regular expressions work within a SQL query<BR><BR>Depends on the database. Access, no. MySQL, yes. SQL Server, pretty limited.<BR><BR>BUT...<BR><BR>But each of those DBs has some reasonably useful string functions, and you might be able to do this in some way other than a RegEx if you would care to explain what you are after.<BR><BR>************<BR><BR>Do you have a suggestion as to where to ask SQL questions?<BR><BR>Well, obviously the Database forum here, instead of this generic forum.<BR><BR>For SQL Server specific questions, look at www.SQLTeam.com<BR><BR>***********<BR><BR>I&#039;m not very familiar with SQL so I will look at the UNION query and see if that&#039;s a better solution<BR><BR>*IF* the two (or more) Recordsets you are combining into the array-of-arrays are homogeneous (same number of fields, same data types and same implicit data meanings on a field-by-field basis), then I guarantee you that UNION is the better way.<BR><BR>Big IF.<BR><BR><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
•