Home Segments Index Top Previous Next

392: Mainline

You easily can define arrays with more than one dimension by using arrays as array elements. For example, to define weekOfCalories with seven rows (for the days of the week) and three columns (for breakfast, lunch, and dinner calories), you first create an array of seven elements, assigned, say, to Calories:

Calories := Array new: 7 

Next, you arrange for each element in the array to be an array of three elements:

(Interval from: 1 to: 7) 
  do: [:index | Calories at: index put: (Array new: 3)] 

To retain 850 as the number of calories in the second meal of the fourth day, you send one at: message to get the correct array element and one at:put: message to set the appropriate element in that array element:

              *-- Row        *-- New value 
              |              | 
              v              v 
(Calories at: 4) at: 2 put: 850 
                     ^ 
                     | 
                     *-- Column       

Later, you recover that element with two at: messages:

(Calories at: 4) at: 2