CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
frequency count of words...

 



sajanagr
Novice

Oct 2, 2008, 10:11 PM

Post #1 of 6 (4984 views)
frequency count of words... Can't Post

i need to generate a code tat wil count the frequency of every word in a file..
can anyone tel me how i can perform this.. ??
Not all who wander are lost ---- /me


KevinR
Veteran


Oct 2, 2008, 10:18 PM

Post #2 of 6 (4983 views)
Re: [sajanagr] frequency count of words... [In reply to] Can't Post

A google search will probably find you some code as this is a very typical task and is often asked on forums.
-------------------------------------------------


maestria
Novice


Oct 4, 2008, 2:48 AM

Post #3 of 6 (4949 views)
Re: [KevinR] frequency count of words... [In reply to] Can't Post

Put your file to be checked in 'test'.
Edit the below code according to your requirement.

#!/usr/bin/perl -l
open(FILE,"test");
while($line=<FILE>){
chomp ($line);
@arr=split(/ /,$line);
foreach $var(@arr){
if(exists $ansresult{$var}){
$ansresult{$var}++;
}
else{
$ansresult{$var}=1;
}
}
}
while(($value,$key)=each %ansresult){
print "$value Appears $key times";
}

------------------------------------------------------------------
If some one can do it, we can do it better
http://maestriatech.com
perl@maestriatech.com
--------------------------------------------------------------------


KevinR
Veteran


Oct 4, 2008, 4:37 AM

Post #4 of 6 (4941 views)
Re: [maestria] frequency count of words... [In reply to] Can't Post

this is unecessary and inefficient:


Code
 if(exists $ansresult{$var}){ 
$ansresult{$var}++;
}
else{
$ansresult{$var}=1;
}


all thats needed in place of that is:


Code
$ansresult{$var}++;

-------------------------------------------------


FishMonger
Veteran / Moderator

Oct 6, 2008, 2:03 PM

Post #5 of 6 (4773 views)
Re: [maestria] frequency count of words... [In reply to] Can't Post

In addition to the unnecessary if/else block that Kevin pointed out, there's also no reason/need to use the @arr array.


Code
#!/usr/bin/perl -l

Why are you using -l for no reason?

Every Perl script you write should include the following 2 pragmas.

Code
use strict; 
use warnings;


Code
open(FILE,"test");

Since Perl 5.6 the recommended and best practice is to use a lexical var for the filehandle instead of the bareword and you shold use the 3 arg form of open. Also, you should ALWAYS check the return code of an open call and take action if it fails. Part of that action would be to include in the error message the reason it failed i.e., the value in the $! var.

open my $FILE, '<', 'test' or die "can't open 'test' $!";


Quote
while($line=<FILE>){

better written as:
while( my $line = <$FILE> ) {


Code
        chomp $line; 
@arr=split(/ /,$line);
foreach $var(@arr){
if(exists $ansresult{$var}){
$ansresult{$var}++;
}
else{
$ansresult{$var}=1;
}
}
}

Better written as:

Code
        chomp ($line); 
$ansresult{$_}++ for split /\s+/, $line;


Code
while(($value,$key)=each %ansresult){ 
print "$value Appears $key times";
}

better writtten as

Code
while(  my ($key, $value) = each %ansresult ) { 
print "$key Appears $value times\n";


Complete script:

Code
#!/usr/bin/perl 

use strict;
use warnings;

my %ansresult;

open my $FILE, '<', 'report' or die "can't open 'test' $!";
while( my $line = <$FILE> ) {
next if $line =~ /^\s*$/; # I added this just incase there where blank lines.
chomp $line;
$ansresult{$_}++ for split /\s+/, $line;
}

while( my ($key, $value) = each %ansresult ) {
print "$key Appears $value times\n";
}



(This post was edited by FishMonger on Oct 6, 2008, 2:07 PM)


maestria
Novice


Oct 6, 2008, 10:42 PM

Post #6 of 6 (4740 views)
Re: [FishMonger] frequency count of words... [In reply to] Can't Post

Hi,

Thanks a lot for the education.
I will take care of these things every time i write a script.

------------------------------------------------------------------
If some one can do it, we can do it better
http://maestriatech.com
perl@maestriatech.com
--------------------------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives