At 6:30 PM +0200 6/9/01, allan wrote:
>hi
>consider the script below. it counts every single occurence of a search
>pattern in a txt file.
>i want to count how many times a certain pattern appears in a very large
>file (100.000 lines for instance).
>macperl will run out of memory, so i thought there might be a faster way ...
>thanks

Don't slurp.
>open (INPUT, $inputfile);
>{local $/;$string = <INPUT>;}

Instead, grab & discard each line:

#!/usr/bin/perl -w

my $inputfile = "input.txt";
my $count = 0;
my $search = 'html';

open (INPUT, $inputfile);
while (<INPUT>) {
   while (/$search/g) { $count++ }
}

print  "found $count occurences of: $search";
__END__

HTH

1;

Reply via email to