4.9.2 Reading A Whole File In One Read Operation
4.9.2 Reading A Whole File In One Read Operation
If $/ does not have a defined value, there is no input record separator and the whole file is read in one shot even in scalar context. In the following program, we find the file with the maximum and minimum number of words from among the list of files given as command line argument.
Program 4.38
#!/usr/bin/perl
undef $/;
$maxWordCount = 0;
$maxFileName = "";
$minWordCount = 10000000;
$minFileName = "";
for ($i = 0; $i <= $#ARGV; $i++){
open (IN, $ARGV[$i]);
$file = ;
@fileWords = split /\W*\s+\W*/, $file;
if ($#fileWords > $maxWordCount){
$maxWordCount = $#fileWords;
$maxFileName = $ARGV[$i];
}
if ($#fileWords < $minWordCount){
$minWordCount = $#fileWords;
$minFileName = $ARGV[$i];
}
}
print "The maximum number of words in a file = $maxWordCount\n";
print "The file with maximum number of words = $maxFileName\n";
print "The minimum number of words in a file = $minWordCount\n";
print "The file with minimum number of words = $minFileName\n";
In the program we undefine the value of $/ by using the built-in function undef. We initialize the maximum word count to 0 and the minimum word count to a large number assuming no file has more words than this number. We also initialize the identity of the files with maximum and minimum number of words to the empty string.
Next, we loop through every file given as command line argument. We open each file for reading, and read the whole file at once. We split the file into its individual words. We compare with the maximum and minimum word counts so far and reset them if necessary. If we reassign a value to the maximum or the minimum count, we remember the name of the file. Finally, outside the loop, we print which file has the maximum number of words and which file has the minimum number of words. We also print the maximum number of words and the minimum number of words.
A call to this program looks like the following.
%fileWords.pl paper*
The output of the program looks like the following.
The maximum number of words in a file = 17587 The file with maximum number of words = paper1.tex The minimum number of words in a file = 2345 The file with minimum number of words = paper2.txt
We note that the number of words we see printed above is different from the number of words returned by the Unix command wc because the word splitting pattern we use is different from what Unix uses.
