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 Message-----
> From: Martijn van Exel [mailto:[EMAIL PROTECTED]]
> Sent: Monday, July 16, 2001 8:55 AM
> To: [EMAIL PROTECTED]
> Subject: # of lines in file
> 
> 
> Subscribers,
> 
> 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)? 
> 
> Thanks for your insights.
> 
> $_ contains the filename in both snippets.
> 
> # code snippet 1
> open(FILEH, "database/$_") or die "$!";
> while (sysread FILEH, $buffer, 4096) {
>  $lines += ($buffer =~ tr/\n//);
> }
> close FILEH;
> $lines = 0 if !$lines;
> print " - $lines entries\n";
> 
> #code snippet 2
> open(FILEH, "database/$_") or die "$!";
> @temp = FILEH;
> close FILEH;
> $lines = @tempfile;
> $lines = 0 if !$lines;
> print " - $lines entries\n";
> 
> -- 
> Martijn van Exel
> WEBkitchen - http://www.webkitchen.nl
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to