M
MSG
Suppose I had a big big text file and I needed to count the number
of lines. I have two questions about it:
(A). Perldoc FAQ suggests counting "\n" like this:
while (sysread FILE, $buffer, 4096) {
$lines += ($buffer =~ tr/\n//);
but in Perl Cookbook, the buffer becomes 2*20 (1Mb).
I am sure the authors didn't choose those numbers completely
arbitrarily, so the question is: how did they come up with the
different numbers, or how should a programmer go about choosing
the "right" number?
(B). How does the above method compare to the following code,
which simply uses the automatic line number?
(also from Perl Cookbook)
1 while ( <FH> );
print $. ;
of lines. I have two questions about it:
(A). Perldoc FAQ suggests counting "\n" like this:
while (sysread FILE, $buffer, 4096) {
$lines += ($buffer =~ tr/\n//);
but in Perl Cookbook, the buffer becomes 2*20 (1Mb).
I am sure the authors didn't choose those numbers completely
arbitrarily, so the question is: how did they come up with the
different numbers, or how should a programmer go about choosing
the "right" number?
(B). How does the above method compare to the following code,
which simply uses the automatic line number?
(also from Perl Cookbook)
1 while ( <FH> );
print $. ;