On Aug 20, 2012, at 1:39 PM, Rajeev Prasad wrote:

> thank you. seek did the job.
> 
> by the way can this be made any better?
> 
> just want to find out in how many records string was found:
> 
>             my $count=0;
>             seek $tmp_FH,0,0;
>             while (<$tmp_FH>)
>             {
>                 my $line=$_;chomp($line);
>                 if ($line=~m/\"$str\"/) {$count++;}        #in the file $str 
> string would be in quotes....
>             }


Two possible efficiency improvements:

1. Since you are just searching each line for a string, you can skip the 
chomp($line).

2. Searching for a fixed string might be done faster by using the index() 
function rather than invoking the regular expression engine:

        $count++ if index($line,"\"$str\"") != -1;

Neither of these modifications is likely to produce a significant speed-up. You 
should benchmark them to see what the difference actually is to see if it is 
worth it in your case. The speed bottleneck will still be reading the file from 
a physical device. 

If possible, you should combine searching for the string with whatever 
processing you are doing on the file during the first read, and only read the 
file once.


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to