Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

developer:scriptsamples:multidimensional [2015/09/14] (current)
Line 1: Line 1:
 +====== Multidimensional Arrays ======
 +> **Developer:​** //​[[developer:​rhinoscript|RhinoScript]]//​
 +> **Summary:​** //Discusses rectangular and ragged multidimensional arrays.//
 +
 +=====Question=====
 +Why are there two types of multidimentional arrays? What is the difference between the (x)(y) and (x,y) notation?
 +
 +=====Answer=====
 +VBScript supports two kinds of multidimensional arrays, called **rectangular** and **ragged**. A rectangular array is, well, rectangular. For example:
 +
 +<code vb>
 +Dim MyArray(3,​2) ​
 +</​code>​
 +
 +and you get:
 +
 +<​code>​
 +(0,0) (0,1) (0,2)
 +(1,0) (1,1) (1,2)
 +(2,0) (2,1) (2,2)
 +(3,0) (3,1) (3,2)
 +</​code>​
 +
 +which makes a nice rectangle. A three-dimensional array makes a rectangular prism, and so on up into the higher dimensions.
 +
 +A common practice used by RhinoScript is to simulate multidimensional arrays by making an array of arrays known as **ragged** or **nested** arrays. For example:
 +
 +<code vb>
 +Dim MyArray
 +MyArray = Array(Array(1,​ 2, 3), Array(4, 5), Array(6, 7, 8, 9))
 +</​code>​
 +
 +And so dereferencing the outer array gives you the inner array, which can then be dereferenced itself:
 +
 +<code vb>
 +Rhino.Print MyArray(2)(0) ' 6
 +</​code>​
 +
 +But, you notice something about the indices if we write them out as before:
 +
 +<code vb>
 +(0)(0) (0)(1) (0)(2)
 +(1)(0) (1)(1)
 +(2)(0) (2)(1) (2)(2) (2)(3)
 +</​code>​
 +
 +The indices make a ragged pattern, not a straight rectangular pattern. It is possible to create ragged higher dimensional,​ but allocating all the sub-arrays can be difficult.
 +
 +Thus, in VBScript if you say:
 +
 +<code vb>
 +MyArray(2,​3)
 +</​code>​
 +
 +then you are talking to a rectangular two-dimensional array. And, if you say:
 +
 +<code vb>
 +MyArray(2)(3)
 +</​code>​
 +
 +then you are talking to a one dimensional array that contains another one dimensional array.
 +
 +{{tag>​Developer RhinoScript}}
  
developer/scriptsamples/multidimensional.txt ยท Last modified: 2015/09/14 (external edit)