in reply to Sort text by Chapter names
You could read the text into a hash keyed by the chapter title then sort that
use strict; use warnings; my %chapters; my $chapter_title; while ( <DATA> ) { chomp; if ( m/^Chapter \w+/ ) { $chapter_title = $_; next; } next unless $chapter_title; $chapters{$chapter_title} .= $_; } for ( sort {$a cmp $b} keys %chapters ) { print "$_\n$chapters{$_}\n"; } __DATA__ Chapter One There were lots of monkeys here and they ate all the bananas... lots more text up to hundreds of words. Chapter Nine This chapter has probably 1000 words. Chapter Two Here is the text in the second chapter... Chapter Five Here is the text in the fifth chapter... every chapter is of differing length, some long some short.
In Section
Seekers of Perl Wisdom