View Full Version : Clarification on Array() syntax...
parallon
08-19-2009, 03:45 PM
Hello all. I just need some clarification on VBScript arrays and JS arrays.
I have been told the following:
In VBScript, the columns should be the FIRST subscript and the rows the second one. Otherwise, you can't use Redim Preserve to resize the array! (Redim Preserve only allows you to *change* the LAST dimension of the array...which is why traditionally the rows are last.)
... which I have verified in some places, but then there are sites that say the opposite, with this MSDN article being one of them, Array(Row, Column).
Here is the excerpt:
For example, you can access the first element of the second row of the two-dimensional array by specifying indexes (1, 0).
... and the link to the page:
http://msdn.microsoft.com/en-us/library/02e7z943.aspx
Is this a difference between VB and VBScript?
Also, what about JavaScript? I have always assumed it was Array(Row, Column).
Thanks in advance,
Parallon
Old Pedant
08-19-2009, 09:47 PM
Actually, there is no "rule" about which is the row and which is the column. In *any* language, you as the programmer can decide.
The "rule" in VBScript (and, yes, it's true in VB as well, even VB.NET) all comes about because of the limitation of the ReDim Preserve statement, which *only* allows a change to the last dimension.
I could certainly conceive of a situation where you have a fixed number of row and a variable number of columns, in which case indeed you would want to use arrayName(row,column) so that you could change the column count.
But you will surely admit that's the exceptional case. Most of the time, when we talk about resizing an array, we are talking about changing the number of rows.
Not so incidentally, perhaps the other HUGE motivation behind the data(column,row) notation is that this is the form that the immensely useful and popular ADODB.Recordset.GetRows( ) method call produces! You don't have a choice. If you do something like:
Set RS = conn.Execute( "SELECT name, age, title FROM people" )
data = RS.GetRows( )
' then you MUST refer to the array elements via (examples):
name = data( 0, rownumber )
title = data( 2, rownumber )
Now, Microsoft deprecates the use of Redim Preserve in VB.NET, so I suppose they can use that as justification for the article you cited.
But as you can clearly see, the statement is still present in VB.NET 3.5:
http://msdn.microsoft.com/en-us/library/w8k3cys2.aspx
Resizing with Preserve. If you use Preserve, you can resize only the last dimension of the array, and for every other dimension you must specify the same bound it already has in the existing array.
Personally, I think it's a stupid limitation. It would take some programmer maybe all of 20 minutes to write a version of ReDim Preserve that would allow modifications to any and all of the dimensions of an array. Just because VB6 had that limitation in the dark ages is no reason for it to still exist today. But it does. And so long as it does, it make more sense to think of a 2D array as being indexed by columns first and then rows.
Old Pedant
08-19-2009, 09:52 PM
Oh, yes...And JavaScript does not even *HAVE* multidimensional arrays!
Oh, you frequently see code such as
var whatever = someArray[rownumber][columnnumber];
But what you are *ACTUALLY* seeing there is an ARRAY-OF-ARRAYS.
It's perfectly legal in JS code (and in C and C++ and Java and, actually, even in VB and VBScript!) to create arrays thus:
var outer = new Array( );
outer[0] = ["an","array","of","strings"];
outer[1] = [37, 92, 40, 12, 101, 202, 441, 555];
outer[2] = [3.1415, 2.71828];
outer[3] = "Not an array at all";
// and then you code
var s = outer[0][2]; // "of"
var i = outer[1][4]; // 101
var oops = outer[3][2]; // NO SUCH ELEMENT! Error!
...
parallon
08-20-2009, 11:18 PM
Thank you so much, that definately helped a lot.
Mike
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.