On Jul 16, Martijn van Exel said:
>I'm trying to get a line count for a number of files. Is sysread() the
>preferred (fastest, most efficient) way to go (snippet 1) or should I
>read the file into an array and count the array instead (something like
>snippet 2, untested)?
Do NOT read a file
Don't do the "put a file into an array" trick if you're not VERY sure the
file is small.
It's generally concidered bad programming practice and a recipe for disaster
if you dont control the input.
having said that, i'd like to note that there's also a memory penalty for
using an array over a stri
As sysread() doesn't use buffers, it's probably more efficient to use normal
reads. Also, splitting lines by yourself (i.e. in Perl) won't be as efficient
as in C implementation. Thus I recommend to use
open(FILEH, "database/$_") or die "$!";
my @tempfile = ;
close FILEH;
my $lines = scalar @te
here is a routine that I wrote that I use in many scripts as a quick line
count:
sub wc
{
my $file = shift;
my $numlines;
open(FILE,"$file") or die "$file :$!";
$numlines += tr/\n/\n/ while sysread(FILE, $_, 2 ** 16);
return($numlines);
}
> -Original Me