Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Sort text by Chapter names

by rnewsham (Curate)
on May 24, 2018 at 05:36 UTC ( #1215130=note: print w/replies, xml ) Need Help??

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1215130]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2020-11-30 05:04 GMT
Find Nodes?
    Voting Booth?

    No recent polls found