Home Segments Index Top Previous Next

460: Mainline

Now, suppose that you have read information into an OrderedCollection instance using the method defined in Segment 456. You can convert that OrderedCollection instance into a SortedCollection instance in which the food descriptions are sorted by name. Then, you can arrange for each food to be written into a file, using asString, introduced in Segment 71, to convert calorie numbers into strings. Finally, you send a cr message to terminate the line:

Food method definition • instance 
printFoodLine: aStream 
  aStream nextPutAll: 
    name , ' ' ,  
    fCalories asString , ' ' , 
    cCalories asString , ' ' , 
    pCalories asString ; 
    cr 
Food method definition • class 
outputFoods: foodCollection to: aFile 
  | outputStream | 
  outputStream := File newFile: aFile. 
  foodCollection do:  
    [:element | element printFoodLine: outputStream]. 
  outputStream close 
Workspace
Food outputFoods: ((Food collectFrom: 'c:\test\vtbls.dta') 
                    asSortedCollection: [:x :y | x name <= y name]) 
     to: 'c:\test\foods.srt' 

The result, in the file \test\foods.srt, is as follows:

Broccoli 3 16 9 
Cabbage 2 14 3 
Carrots 1 33 4 
Corn 1 23 6