Creating and Using 1-D Arrays
Definition
Arrays are data structures that hold a collection of elements. One can think of an array as a container of boxes. The boxes are called the 'members' of the array. The key thing about arrays is that they have indexes to access each element stored in it. Arrays are critical to managing information in a script.
Declaration
A basic array declaration looks like this:
ReDim arrayName(numberOfElements)
' declaring an array with 4 slots
ReDim myArray(3)
Assigning Values
' assigning values to each position
myArray(0) = 10.9
myArray(1) = 20.0
myArray(2) = "hey"
myArray(3) = 5
A quicker way to define arrays is to declare them and assigning them in a single line:
Dim quickArray : quickArray = (10.0, 20, "hey", 5)
Rhino.Print quickArray(0)
Rhino.Print quickArray(1)
>> Command : 10.0
>> Command : 20
Accessing Values
' printing some values
Rhino.Print myArray(0)
Rhino.Print myArray(2)
Rhino.Print myArray(1)
>> Command: 10.9
>> Command: "hey"
>> Command: 20.0
' re-writing positions in the array
myArray(2) = "new string here"
Rhino.Print myArray(2)
>> Command : new string here
Dim aVariable: aVariable = 70
myArray(2) = a
Rhino.Print myArray(2)
>> Command : 70
Dim quickArray : quickArray = Array(10.0, 20, "hey", 8)
Rhino.Print quickArray(0)
Rhino.Print quickArray(1)
>> Command : 10.0
>> Command : 20
Using iteration to populate a 1-D array
What if instead of 4 values we need to store 400? Declaring an array and assigning all of its values is not the usual way to work with arrays.Most of the times we want an array to store values that are product of a computation, or an evaluation of some elements of the script or file. In order to assign computed values to an array we can use a structure called 'For Loop'. This structure allows us to perform some operations iteratively, that is, a defined number of times. The following lines of code create an array and then assign values to all the members of the array using a 'For Loop' structure.
redim arrayToFill(1000) ' declaring the array and its size
Dim i ' this variable acts as a counter
for i = 0 to UBound(arrayToFill) 'UBound means Upper Bound
' do something here
arrayToFill(i) = i
Rhino.Print i
Next
What happens if you run this code? Answer: A list of numbers is printed in reverse order, from 1000 to 0. If the For Loop confuses you check the control flow section for a more detailed explanation iteration structures.
Points as Arrays
redim myPoint(2) ' declaring the array and its size
myPoint(0) = 10.0 ' X coordinate
myPoint(1) = 8.0 ' Y coordinate
myPoint(2) = 12.0 ' Z coordinate
Rhino.AddPoint myPoint
Dim i
for i = 0 to 100
Rhino.AddPoint Array(i, 7.0, 10.0)
Next
For each integer between 0 and 100
Add a point in an X location relative to the current integer,
keeping the Y and Z coordinates constant
Arrays of arrays
Since arrays can store different types of objects, nothing prevents us from creating an array of arrays. For example:ReDim myArrayOfArrays(10)
myArrayOfArrays(0) = array(5,10,3)
myArrayOfArrays(1) = array(2,2,1)
myArrayOfArrays(2) = array(5,3,1)
'access
Rhino.print myArrayOfArrays(0)(1)
Rhino.print myArrayOfArrays(1)(2)
Rhino.print myArrayOfArrays(2)(2)
Rhino.print myArrayOfArrays(2)(3)
>> Command: 10
>> Command: 1
>> Command: 1
>> Subscript out of range error
Values stored in this structures can also be put inside other variables
Dim myVar, myVar2, myVar3
myVar = myArrayOfArrays(0)(1)
myVar2 = myArrayOfArrays(1)(2)
myVar3 = myArrayOfArrays(2)(3)
Rhino.print myVar
Rhino.print myVar2
Rhino.print myVar3
>> Command: 10
>> Command: 1
>> Subscript out of range error