Learning Perl Objects, References & ModulesLearning Perl Objects, References & ModulesSearch this book

11.8. Exercise

The answers for all exercises can be found in Section A.10.

11.8.1. Exercise [30 min]

The Professor has to read a log file that looks like:

Gilligan: 1 coconut
Skipper: 3 coconuts
Gilligan: 1 banana
Ginger: 2 papayas
Professor: 3 coconuts
MaryAnn: 2 papayas
...

He wants to write a series of files, called gilligan.info, maryann.info, and so on. Each file should contain all the lines that begin with that name. (Names are always delimited by the trailing colon.) At the end, gilligan.info should start with:

Gilligan: 1 coconut
Gilligan: 1 banana

Now the log file is large, and the coconut-powered computer is not very fast, so he wants to process the input file in one pass and write all output files in parallel. How does he do it?

Hint: Use a hash, keyed by the castaway name, holding IO::File objects for each output file. Create them as necessary.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.