interactive scripting - CGR C/092 - fall 2004
Philip van Allen - v a n a l l e n @ a r t c e n t e r . e d u

room 142, Monday 4:00pm-7:00pm
all materials on this web site © copyright 2004, Philip van Allen
 
week 7a - arrays, getting user input, quiz example

arrays : 


a filing system for variables

So far, we've used variables that hold just one value. And if we wanted to store more than one value, we created additional variables. But suppose we wanted to store a list of related items like peoples names. Rather than creating different variables for each name, we can use something called an array, which is a single variable that holds more than one value. For example, to store several names using standard variables, we do something like this:

myNamesA = "Joe";
myNamesB = "Mary";
myNamesC = "Kelley";

Which could be diagramed like this:

myNamesA "Joe"

myNamesB "Mary"

myNamesC "Kelley"

Arrays on the other hand have just one name for a series of values. Think of arrays like one of those office mailboxes that is a large cabinet with a bunch of boxes in it for each employee. The name is for the whole cabinet, and each mailbox is identified by a number. Another way to look at it is as a spreadsheet, where the name of the variable is for the whole spreadsheet, and column headings are for the values. In our example above, we would do the following. Note that the first element in an array is always identified by an index of zero, not one:

var myNames = new Array()
myNames[0] = "Joe";
myNames[1] = "Mary";
myNames[2] = "Kelley";

OR, a shorthand for this is:

myNames = ["Joe", "Mary", "Kelley"];



the array index : 

getting to a specific element in the array

In the array, multiple elements are filed inside the array, where each element is identified by the array index, as this diagram shows:

  0 1 2
myNames "Joe" "Mary" "Kelley"

We get the value of a individual elements of an array by referring to its numeric placement in the array with the index. For example, to show Mary, we would use the following code, with the number "1" as the index into the array:

trace(myNames[1]);

A powerful capability of arrays is that variables can be used in place of the array index. Say we wanted to output all of the values in the above array. Rather than hard coding the literal index for each array element, use a loop to increment an index variable "i" from 0 to 2. Zero is the index for the first element in the array (remember, arrays always start at zero), and 2 is the index for the 3rd element in the array :

for (i=0; i <= 2; i++) {
    trace(myNames[i]);
} 

 

 
don't hard-code : 

use variables and calculations to make your code general

As mentioned above, zero is the index for the first element. The last element index is always one less than the total elements in the arrary. We can get the number of elements in the array using the ".length" property.

One use of this is to create a generalized loop that displays all the elements in an array, regardless of the number of elements in that array. Rather than hard-coding the number of array elements in the for loop condition ("i <= 2") as in the example above, write the code so that it calculates the number elements in the array by using the ".length" property of the array. This is better coding practice because the loop works without modification on arrays with different numbers of elements.

As an example of this, suppose we initialize an array with 3 elements, and then add one more element to that array. Use the "push()" method to add elements to the end of the array, and use the ".length" property to calculate the length of the array. Then use a loop to print out the all the elements:

myNames = ["Joe", "Mary", "Kelley"];
myNames.push("Massoud"); // add a name to end of list
lastElement = myNames.length - 1; // calculate the # of elements starting at 0
for (i=0; i <= lastElement; i++) { // loop through all elements in array
    trace(myNames[i]);
}

 

 

all materials on this web site © copyright 2004, Philip van Allen

top