homepage Welcome to WebmasterWorld Guest from 54.237.78.165
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
counting occurance of a particular phrase in a file
Stormer

10+ Year Member



 
Msg#: 3732 posted 7:01 am on Jul 29, 2004 (gmt 0)

Here is my code
for counting occurence of a phrase in a file...
the prob here which i am facing is that when 1st time
the phrase occurs it increments the counter and comes out of the loop and shows we count = 1..
it does go upto end of the loop.

plz help
Stormer

open(FILE, "title.txt");
while (<FILE>) {

if(eof()) { # Not eof().

while($_ =~ m/[\w\W\s]*$in[\w\W\s]*/ig){
$k++;
}
}

print OUT "Total no of times $arr1[$j] occured is : $k \n" ;

print "$arr1[$j] : $k \n" ;
$k=0;
}
}

 

kaled

WebmasterWorld Senior Member kaled us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3732 posted 9:10 am on Jul 29, 2004 (gmt 0)

Your use of eof appears unnecessary (and wrong) - I think you're only scanning the last line of code (when eof is true).

As a side-bar, I believe that if you substitute the search string with a null string, the returned value is the number of substitutions made.

e.g. $x = ($_ =~ s/$search//ig);

Kaled.

PS
I've not used Perl much, so don't take anything above as definitive.

markanthony

10+ Year Member



 
Msg#: 3732 posted 3:46 pm on Aug 16, 2004 (gmt 0)

Make sure that you are in 'use strict' mode.

Declare my $k outside of the loop to ensure that it is not local to the loop as it will be overwritten each time the loop runs as opposed to being incremented.

You should be able to just....

use strict;
my $k;
while(<>)
{
chomp;
if($_ =~ m/[\w\W\s]*$in[\w\W\s]*/ig)
{
$k++;
}
}
print $k;

run from command line as:
perl myscript.pl text.txt

timster

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3732 posted 4:58 pm on Aug 16, 2004 (gmt 0)

A regular expression may not be the most efficient solution here. This would probably run faster:


open (FILE, "file.txt");
$/ = $in;

my $count;
while (<FILE>) {
++$count;
}

print $count;

phaze

10+ Year Member



 
Msg#: 3732 posted 7:22 pm on Aug 16, 2004 (gmt 0)

G'day. If you want to know how many times 'blahblah' appears in test.txt, do this:

perl -ne 'END { print "Word appeared $t times\n" } @w = /(blahblah)/g; $t += @w' test.txt

If you're still confuzled, check this out:
[perl.com...]

And if you just want to know how many lines a phrase appears in, do this:

grep blahblah test.txt wc -l
(Assuming you're using a real operating system like linux of course)

And if you have a whole directory structure with tons of html files and you want to spell your misspelling of mispelled correctly, you can do this:

perl -pi -e 's/mispelled/misspelled/gi' `find . -name "*.html"`

Rock and Roll.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved