in reply to Sort text by Chapter names
G'day Bman70,
Whether this is homework, $work, or something else, you really should make an effort to write some code yourself. We're always happy to help; we're less happy to do your work for you. If it is homework, tell us what part of the language you're currently studying — is this intended to give you an exercise in I/O, sorting, regexes, etc. — we can tailor answers to help you with whatever part you're having difficulties with.
That said, here's my TMTOWTDI:
#!/usr/bin/env perl use strict; use warnings; use autodie; my $text = <<'EOT'; 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. EOT { open my $memfh, '<', \$text; local $/ = 'Chapter '; for (sort <$memfh>) { chomp; next unless length; print $/, $_; } }
Output:
Chapter Five Here is the text in the fifth chapter... every chapter is of differing length, some long some short. Chapter Nine This chapter has probably 1000 words. Chapter One There were lots of monkeys here and they ate all the bananas... lots more text up to hundreds of words. Chapter Two Here is the text in the second chapter...
— Ken
|
|---|