in reply to Extracting a chapter from text file

I haven't seen a solution that uses the .. range operator, discussed in perlop. And I think this is the sort of thing it was designed for. Here's an example:

while( <DATA> ) { my $in_toc = 1 .. /^={5,}/; if( !$in_toc && /^Chapter 2\./ .. /^Chapter 3\./ ) { next if /^Chapter \d+\./; print; } } __DATA__ Table of Contents Chapter 1. Introduction Chapter 2. Main Chapter 3. Conclusion ============================== Chapter 1. Introduction This is the introduction preceding Chapter 2. Chapter 2. Main This is the text contained in Chapter 2 and will contain a lot of text + with at least 100 words and probably somewhere around 1000-5000. Chapter 3. Conclusion This is the conclusion.

This produces the following output:

This is the text contained in Chapter 2 and will contain a lot of text + with at least 100 words and probably somewhere around 1000-5000.

Substitute your own operations in place of 'print'. That probably where you would also include your word-count check.


Dave