Home > Archive > PERL Beginners > November 2005 > Counting word frequency in a text file
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Counting word frequency in a text file
|
|
|
| Someone will probably have an easy solution to this, but I have had the
hardest time finding out how to do it. I have a text file with a few
words and numbers in it separated by space(s). I want to use Perl to
search that file for the frequency of a word or number, say, "4" and
return the the total number of "4's" found. Thanks for any guidance as
I am a newbie. :-)
| |
| usenet@DavidFilmer.com 2005-11-12, 7:55 am |
| Tim wrote:
> I want to use Perl to search that file for the frequency of a word or number...
See perlfaq4 (http://tinyurl.com/8epwq) for some background info on how
to count the number of occurances of a substring within a string.
Since you want to process a file (presumably multiple lines) you would
need to adapt the info in perlfaq4 a bit.
So you could do something like this to count the number of occurances
of the "world" within a file (I'm using the __DATA__ block in place of
a filehandle for this example):
#!/usr/bin/perl
use warnings; use strict;
my $count = 0;
foreach my $string (<DATA> ) {
while ($string =~ /world/g) { $count++ }
}
print "$count\n";
__DATA__
hello world.
The world is a nice place.
A thousand worlds away.
Which world is your world?
##output: 5 (note that "worlds" also matches; you would need to
fine-tune your regular expression if you don't want this behavior)
| |
| Brad Baxter 2005-11-17, 9:56 pm |
| Tim wrote:
> Someone will probably have an easy solution to this, but I have had the
> hardest time finding out how to do it. I have a text file with a few
> words and numbers in it separated by space(s). I want to use Perl to
> search that file for the frequency of a word or number, say, "4" and
> return the the total number of "4's" found. Thanks for any guidance as
> I am a newbie. :-)
As a newbie, you'll be glad to know that many questions you'll be
asking have been asked before. These frequently asked questions are
available in many places including here for one:
http://perldoc.perl.org/index-faq.html
For example:
http://perldoc.perl.org/perlfaq6.ht...on-each-line%3f
http://perldoc.perl.org/perlfaq6.ht...ency-summary%3f
It's not a beginner solution, but there's the old one-liner:
perl -pale '$_{$_}++for@F}{$_="@{[%_]}"' textfile
[color=darkred]
Now is the time for all good men to come to the aid of their party.
The quick brown fox jumped over the lazy dog.
I think that I shall never see a poem lovely as a tree.
[color=darkred]
tree. 1 quick 1 over 1 never 1 men 1 I 2 all 1 poem 1 see 1 think 1
time 1 as 1 the 3 to 2 aid 1 dog. 1 a 2 come 1 their 1 Now 1 of 1 is 1
jumped 1 that 1 for 1 brown 1 lazy 1 fox 1 shall 1 good 1 The 1 party.
1 lovely 1
See also http://perldoc.perl.org/perlrun.html
--
Brad
|
|
|
|
|