topics.03_creating and using 1-D arrays


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)

An array declaration looks a lot like a variable declaration, but notice that instead of Dim we used the word 'ReDim'. This  tells the computer to create space in memory for an array of objects, rather than just an object. If you don't remember how to declare a single variable check it out here. Also notice that there's a value between parentheses, this is the number of elements of the array, or its 'size'. Now let's look at a practical example.

' 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

Notice that array indexes start with zero (0).  If the number of positions in the array is 'n' we need to declare the array having 'n-1'. If you remember this every time you declare an array you will save many hours of debugging.
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)

Notice that when we declare an array using this method we do not use the 'ReDim' keyword. We use the familiar 'Dim'. Other than that this kind of declaration has the same effects as the one described before. It's just quicker.

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)

Running this script will have the following output

>> Command: 10.9
>> Command: "hey"
>> Command: 20.0

Notice how the values we put in the array can be of different data-types, we can put strings, doubles, integers, booleans, etc. It's as if in our carton of eggs we could put also oranges, pasta, and a little toy piano. If you don't remember what data-types are available check it here. We can also overwrite values and put new ones, and even put variables. See the following code.

' 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

A quick way to define arrays is to declare them and assigning them in a single line:

Dim quickArray : quickArray = Array(10.0, 20, "hey", 8)

Notice that when we declare an array using this method we do not use the 'ReDim' keyword. We use the familiar 'Dim'. Other than that this kind of declaration has the same effects as the one described before. It's just quicker.

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

This code creates an array of 1000 slots, creates a variable 'i' as a counter, and iterates from 0 to 1000 assigning the value of the counter 'i' to each member of arrayToFill.
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

Rhinoceros represents points with an array of their coordinates in 3-D space (X, Y and Z). See the following block of code:

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

This is an array with three members, each of which represents the coordinate value (X, Y, or Z) of a point. It is a convention that X is always in the position 0, Y is always in the position 1 and Z is always in the position 2. In order to see the point we need to tell Rhinoceros to plot it. The following line of code passes the just created array 'myPoint' to the Rhino function 'Rhino.AddPoint'.

Rhino.AddPoint myPoint

If we run this code we will see more or less the following:



This may not look very exciting, but understanding how Rhino represents points is actually the key to many of the interesting things we can do. Now, what if we use a for loop to generate the points automatically?

Dim i
for i = 0 to 100
    Rhino.AddPoint Array(i, 7.0, 10.0)
Next

This block of code creates an array of size 100 that will be used to create an equal number of points. See the image below:


If we were to translate from RhinoScript to English we would say:

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

This way of describing scripts is called 'pseudocode' and is a very useful technique before writing the actual code.

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)

Arrays of arrays (also called jagged arrays) are one-dimensional arrays that store other arrays. They are different, therefore, from 2-D arrays.

'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

In the exercise Points in Motion we will mix our knowledge of points, iteration and arrays to generate geometry.




Categories

Configure Widgets