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 kcott (Archbishop)
on May 24, 2018 at 10:25 UTC ( [id://1215144]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 12:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found