Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Sort text by Chapter names

by AnomalousMonk (Archbishop)
on May 24, 2018 at 23:25 UTC ( [id://1215180]=note: print w/replies, xml ) Need Help??


in reply to Sort text by Chapter names

You seem to have solved your original problem by keeping the chapters of the book in order to begin with, but here's another approach to the original problem using "pure" numeric sorting. I think you can extract chapter headers and bodies to a hash already, so I'll start from that point.

c:\@Work\Perl\monks>perl -wMstrict -le "my %xlation = qw( one 1 two 2 three 3 four 4 five 5 six 6 seven 7 eight 8 nine 9 ten 10 thirty 30 thirtyone 31 thirtytwo 32 ); ;; my %chapters = ( 'Chapter One' => 'There were lots of monkeys ...', 'Chapter Nine' => 'This chapter has probably 1000 words.', 'Chapter two' => 'Here is the text in the second chapter...', 'Chapter Thirty-one' => 'Every chapter is of differing length.', 'Chapter Five' => qq{A chapter can have\nlots and lots\nof lines.} +, ); ;; my @ordered_chapters = map undecorate($_), sort map decorate($_), keys %chapters ; ;; my @book = map qq{-: $_ :-\n$chapters{$_}}, @ordered_chapters; ;; print for @book; ;; exit; ;; ;; sub decorate { my ($chapter) = @_; ;; my $rx_c_num = qr{ [[:alpha:]] [-[:alpha:]]+ [[:alpha:]] }xms; ;; my ($n_chapt) = $chapter =~ m{ \A Chapter \s+ ($rx_c_num) \z }xmso or die qq{malformed chapter: '$chapter'}; ;; $n_chapt = normalize_n_chapt($n_chapt); ;; exists $xlation{$n_chapt} or die qq{unnumbered: '$chapter'}; ;; return pack 'n a*', $xlation{$n_chapt}, $chapter; } ;; sub undecorate { return unpack 'x[n] a*', $_[0]; } ;; sub normalize_n_chapt { my ($n_chapt) = @_; ;; $n_chapt =~ s{ - }{}xmsg; return lc $n_chapt; } " -: Chapter One :- There were lots of monkeys ... -: Chapter two :- Here is the text in the second chapter... -: Chapter Five :- A chapter can have lots and lots of lines. -: Chapter Nine :- This chapter has probably 1000 words. -: Chapter Thirty-one :- Every chapter is of differing length.

Update: Note that the existing code can handle chapter numbers like 'ThirtyOne' or 'Thirtyone', but not 'Thirty One'. What changes would be needed to handle such numbers?


Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2024-04-18 10:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found