4.9.1 Reading A Paragraph At A Time
4.9.1 Reading A Paragraph At A Time
Before we get into assigning values to $/, let us look at a special case. If $/ is assigned the empty string as the value, Perl considers itself to be in a paragraph read mode. That is, in a single read operation, instead of reading one single line from a filehandle, Perl reads a full paragraph. This happens with STDIN also. Perl assumes that one or more blank lines delimit a paragraph. In other words, a sequence of at least two \n’s can be considered the input record
separator in the paragraph read mode. Therefore, the assignment
$/ ="";
is somewhat equivalent to the assignment
$/ = "\n\n";
but it is not quite so because one can have more than two newline characters between a paragraph and the next. One can also have other kinds of white space such as \t or the blank space interspersed with the \n character.
Below, we write a program that reads a file paragraph by paragraph. It assumes it is a simple text file. It reports the length of the longest paragraph, in terms of number of words. We do not check if the “words” contain non-alphabetic characters as we have done in some of our earlier programs.
Program 4.37
#!/usr/bin/perl $/ = ""; $file = $ARGV[0]; open (IN, $file); $maxWordCount = 0; while ($paragraph =){ @paraWords = split /\W*\s+\W*/, $paragraph; if ($#paraWords > $maxWordCount){ $maxWordCount = $#paraWords+1;; } } print "The maximum number of words in a paragraph = $maxWordCount\n";
We know that for an array, the last index in the array can be found by replacing the @ in front by $#. Since Perl counts index from zero and not one, we add one to the last index to obtain the number of elements in the array.
The output of the program when called with a file looks like the following.
The maximum number of words in a paragraph = 316
