develooper Front page | perl.beginners | Postings from September 2012

Re: re-reading from already read file handle

Thread Previous | Thread Next
From:
Dr.Ruud
Date:
September 2, 2012 04:29
Subject:
Re: re-reading from already read file handle
Message ID:
20120902112901.12370.qmail@lists-nntp.develooper.com
On 2012-08-20 22:39, Rajeev Prasad wrote:

> 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....
>              }
>

What if the string is in a line twice?

What if the string contains regex meta characters?
(like a dot or an asterisk)

Alternative:

   seek $fh, 0, SEEK_SET;
   my $count;
   $count +=()= /\Q"$str"/g while <$fh>;

-- 
Ruud


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About