Fabulous Adventures In Coding

Eric Lippert's Blog

Running Me Ragged

  A reader asked "Why are there two types of multidimentional arrays? What is the difference between the (x)(y) and (x,y) notation?"

 

Good question.  There are two kinds of multidimensional arrays, called "rectangular" and "ragged".  A rectangular array is, well, rectangular.  You say

 

DIm MyArrary(3,2)

 

and you get an array with indices:

 

(0,0)  (0,1)  (0,2)

(1,0)  (1,1)  (1,2)

(2,0)  (2,1)  (2,2)

(3,0)  (3,1)  (3,2)

 

which makes a nice rectangle.  A three-dimensional array makes a rectangular prism, and so on up into the higher dimensions.

 

Now, as I mentioned earlier, JScript does not have multidimensional arrays.  A clever trick to simulate multidimensional arrays is to make an array of arrays:

 

var x = new Array(

  new Array(1, 2, 3),

  new Array(4, 5),

  new Array(6, 7, 8, 9));

 

And so dereferencing the outer array gives you the inner array, which can then be dereferenced itself:

 

print(x[2][0]); // 6

 

But you notice something about the indices if we write them out as before:

 

[0][0]   [0][1]   [0][2]

[1][0]   [1][1]

[2][0]   [2][1]   [2][2]   [2][3]

 

The indices make a ragged pattern, not a straight rectangular pattern.

 

You can have ragged higher dimensional arrays as well, though allocating all the sub-arrays gets to be a royal pain. 

 

There are often times when you want ragged arrays even in a language that supports rectangular multi-dimensional arrays, so VBScript supports both.  If you say

 

MyArray(2,3)

 

then you are talking to a rectangular two-dimensional array.  If you say

 

MyArray(2)(3)

 

then you are talking to a one dimensional array that contains another one dimensional array. 

Published Monday, September 22, 2003 9:34 PM by Eric Lippert

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Deadprogrammer said:

Thanks a lot. Too bad you never wrote a vbscript book.
September 23, 2003 4:45 PM
 

Eric Lippert said:

I never did write a VBScript book, but I was the technical editor of "VBScript In A Nutshell, 2nd edition". (And boy, did I earn my fee. The first edition has a LOT of mistakes.)
September 23, 2003 7:58 PM
 

Shefali said:

Hi,

I feel lucky to have found your article "Running Me Ragged " on google today.

I was desperately looking for information on ragged arrays in vbscript.

I was trying to achieve a dynamic 3rd dimension for every 1st dimension element in my 3-dimensional rectangular array but it was just not working and now I know why thanks to your article.

I want to use a ragged 3 dimensional array for the above solution instead but am not sure how.

Would you be able to suggest a few links or a book where I can find an example of the ragged 3 dimensional arrays and how to use them?

October 4, 2006 8:54 PM
 

arrMyData(5,5) arrMyData(5)(5) said:

February 12, 2008 1:40 PM
 

tolga said:

i need some help..

i wasted a lot of time while searching on the internet about my problem and i am not sure if i am writing on the correct place.

My problem is ;

** i am a vb 6.0 user

i have a 3D-D array

-myArray()

and i want to redim. it but i dont want to use a prism. So ;

i assigned NJ(1)=10 ; NJ(2)=14; NJ(3)=5

NI(1)=10 ; NI(2)=18; NI(3)=5 and i want to redim myarray like ;

myarray(1,NI(1),NJ(1))

myarray(2,NI(2),NJ(2))

myarray(3,NI(3),NJ(3))

for computing economy i dont need want to dim. my array like ; redim myarray(3,14,18)

how can i do that ?

thanks ; Tolga

April 14, 2008 6:38 PM

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required
Submit

About Eric Lippert

Eric Lippert is a senior developer on the Microsoft C# compiler team. Before that he worked on the framework of Visual Studio Tools For Office. Before that, he worked on the compilers, runtimes and tools for VBScript, JScript, Windows Script Host and other Microsoft Scripting technologies. He lives in Seattle and spends his free time editing books about programming languages, playing the piano, and trying to keep his tiny sailboat upright in Puget Sound.

This Blog

Syndication


© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker