From 9f7335a93e11db48d054af17a8acd99483ef9afe Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Fri, 7 Jan 2011 00:20:13 -0500 Subject: [PATCH] counting number of lines per chapter and displaying --- cli/alice-in-wonderland.txt | 24 +++++++++---------- cli/ex03/rwfakegrep.pl | 46 +++++++++++++++++++++++++++++++++---- 2 files changed, 54 insertions(+), 16 deletions(-) diff --git a/cli/alice-in-wonderland.txt b/cli/alice-in-wonderland.txt index 6c89ee6..4565d28 100644 --- a/cli/alice-in-wonderland.txt +++ b/cli/alice-in-wonderland.txt @@ -36,7 +36,7 @@ THE MILLENNIUM FULCRUM EDITION 3.0 -CHAPTER I. Down the Rabbit-Hole +CHAPTER 1. Down the Rabbit-Hole Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the @@ -248,7 +248,7 @@ So she set to work, and very soon finished off the cake. -CHAPTER II. The Pool of Tears +CHAPTER 2. The Pool of Tears 'Curiouser and curiouser!' cried Alice (she was so much surprised, that for the moment she quite forgot how to speak good English); 'now I'm @@ -449,7 +449,7 @@ way, and the whole party swam to the shore. -CHAPTER III. A Caucus-Race and a Long Tale +CHAPTER 3. A Caucus-Race and a Long Tale They were indeed a queer-looking party that assembled on the bank--the birds with draggled feathers, the animals with their fur clinging close @@ -693,7 +693,7 @@ back to finish his story. -CHAPTER IV. The Rabbit Sends in a Little Bill +CHAPTER 4. The Rabbit Sends in a Little Bill It was the White Rabbit, trotting slowly back again, and looking anxiously about as it went, as if it had lost something; and she heard @@ -954,7 +954,7 @@ hookah, and taking not the smallest notice of her or of anything else. -CHAPTER V. Advice from a Caterpillar +CHAPTER 5. Advice from a Caterpillar The Caterpillar and Alice looked at each other for some time in silence: at last the Caterpillar took the hookah out of its mouth, and addressed @@ -1250,7 +1250,7 @@ had brought herself down to nine inches high. -CHAPTER VI. Pig and Pepper +CHAPTER 6. Pig and Pepper For a minute or two she stood looking at the house, and wondering what to do next, when suddenly a footman in livery came running out of the @@ -1567,7 +1567,7 @@ wish I'd gone to see the Hatter instead!' -CHAPTER VII. A Mad Tea-Party +CHAPTER 7. A Mad Tea-Party There was a table set out under a tree in front of the house, and the March Hare and the Hatter were having tea at it: a Dormouse was sitting @@ -1906,7 +1906,7 @@ fountains. -CHAPTER VIII. The Queen's Croquet-Ground +CHAPTER 8. The Queen's Croquet-Ground A large rose-tree stood near the entrance of the garden: the roses growing on it were white, but there were three gardeners at it, busily @@ -2209,7 +2209,7 @@ looking for it, while the rest of the party went back to the game. -CHAPTER IX. The Mock Turtle's Story +CHAPTER 9. The Mock Turtle's Story 'You can't think how glad I am to see you again, you dear old thing!' said the Duchess, as she tucked her arm affectionately into Alice's, and @@ -2526,7 +2526,7 @@ tone: 'tell her something about the games now.' -CHAPTER X. The Lobster Quadrille +CHAPTER 10. The Lobster Quadrille The Mock Turtle sighed deeply, and drew the back of one flapper across his eyes. He looked at Alice, and tried to speak, but for a minute or @@ -2825,7 +2825,7 @@ came, carried on the breeze that followed them, the melancholy words:-- -CHAPTER XI. Who Stole the Tarts? +CHAPTER 11. Who Stole the Tarts? The King and Queen of Hearts were seated on their throne when they arrived, with a great crowd assembled about them--all sorts of little @@ -3082,7 +3082,7 @@ name 'Alice!' - CHAPTER XII + CHAPTER 12 Alice's Evidence diff --git a/cli/ex03/rwfakegrep.pl b/cli/ex03/rwfakegrep.pl index eaef5ec..16cf4ea 100644 --- a/cli/ex03/rwfakegrep.pl +++ b/cli/ex03/rwfakegrep.pl @@ -3,6 +3,7 @@ use strict; use warnings; use File::Basename; +use English qw/$PROGRAM_NAME/; my $basename = basename($0); my $USAGE = < USAGE ; -open(INFILE, '<', $ARGV[0]) or die "$USAGE\n$!"; -open(OUTFILE, '>', $ARGV[1]) or die "$USAGE\n$!"; -foreach my $inline () { - print OUTFILE $inline; +sub show_last_chapter_with_nlines { + my ($ch, $nlines) = @_; + print "Chapter $ch has $nlines lines\n"; } +sub main { + open(INFILE, '<', $ARGV[0]) or die "$USAGE\n$!"; + open(OUTFILE, '>', $ARGV[1]) or die "$USAGE\n$!"; + + my $have_seen_firstchapter = 'no'; + my $nlines = 0; + my $chline = ''; + my $ch = ''; + + foreach my $inline () { + if ($inline =~ /^\s*CHAPTER.*$/) { + + $chline = "$inline"; + $ch = "$chline"; + $ch =~ s/^\s*CHAPTER\s*([0-9]*).*/$1/; + $ch =~ s/[\r\n]//; + + if ($have_seen_firstchapter eq 'yes') { + show_last_chapter_with_nlines($ch - 1, $nlines); + $nlines = 0; + } else { + $have_seen_firstchapter = 'yes'; + $nlines = 0; + } + print OUTFILE $inline; + } + $nlines++; + } + + show_last_chapter_with_nlines($ch, $nlines); + +} + + +if ($PROGRAM_NAME eq __FILE__) { + main(); +} + 1; __END__