Home Segments Index Top Previous Next

480: Mainline

Now, you know how to extract strings from a file and how to test particular characters. Next, you need to learn how to read information from the appropriate strings.

The simplest way to read information from a string is to covert it into a stream. Then, you can extract information from it using nextWord—the same method that you used before to extract information from a file stream.

In the following fragment, for example, the value of the variable, stream, is a stream that you produce by sending asStream to a string. With stream's value established, you can use messages similar to those used in Segment 456 to extract the information needed to create a Food instance and to add that instance to a collection assigned to collection:

stream := string asStream. 
stream nextWord. 
collection add:  
  (Food new name: stream nextWord; 
    fCalories: stream nextWord asInteger; 
    cCalories: stream nextWord asInteger; 
    pCalories: stream nextWord asInteger. 

The fragment skips over the type code and ignores everything in the string after the protein-calories number.