in reply to Faster way?
We move the RECENT file stuff outside of your loop, since it makes little sense to keep re-opening the file for every line we want to write. I imagine that's a major source of your speed problems. Since $search_value doesn't change, we optimize the regular expression with the /o switch. If you wanted to forget about subsequent matches in a given file, you could add a last statement inside your loop, which would skip to the next file instead of reading the rest of the current file, but it seems like you're interested in each line that matches.chomp(@file_array); open(RECENT, ">>$installpath/recent"); # or just > foreach $filename (@file_array) { open(FH, "< $filename") or warn "Could not open $filename: $!"; while (<FH>) { if (/$search_value/o) { print RECENT "$search_value=$filename--> $_"; $numhash{$search_value}++; $matched++; } } close(FH); } close(RECENT);
If your 'recent' file is a temporary/transient thing, used only for processing later in your script, you might also want to consider just storing your matches in an internal data structure, and use them later instead of reading from your file:
Of course, don't underestimate the simplicity of doing this without Perl, if that's all you have to do. The 'grep' command can perform this task natively, unless you need to do some additional processing on the data, and aren't just building a 'recent' file with text matches.push(@{$search_results{$search_value}->{$filename}}, $line);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: Re: Faster way?
by extremely (Priest) on Oct 07, 2000 at 02:31 UTC | |
|
RE: Re: Faster way?
by the_slycer (Chaplain) on Oct 06, 2000 at 22:21 UTC | |
by Fastolfe (Vicar) on Oct 06, 2000 at 22:27 UTC | |
by the_slycer (Chaplain) on Oct 06, 2000 at 23:37 UTC | |
by Fastolfe (Vicar) on Oct 06, 2000 at 23:54 UTC |