Home Segments Index Top Previous Next

378: Mainline

To use an array, once it is created, you need to know how to store into the array and to retrieve from the array at locations identified by an integer index.

To store data into an Array instance, you use the at:put: message with the at: argument providing the integer index and the put: argument providing the value. The following expression, for example, stores an integer into the array assigned to calories at the location indexed by the value of another variable, named counter:

  *-- Global variable with an Array instance assignment 
  | 
  |            *-- Local variable with an integer assignment 
  |            |  
  |            |           *-- Value to be stored 
  |            |           | 
  v            v           v 
Calories at: counter put: 570 

To retrieve data from an array, once the data have been stored, you send the at: message with an integer. The following expression, for example, yields the data stored in the location indexed by the value of the counter variable:

  *-- Global variable with an Array instance assignment 
  | 
  |            *-- Local variable with an integer assignment 
  |            |  
  v            v      
Calories at: counter