Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting words in text

Status
Not open for further replies.

AlexeiD

Programmer
Nov 24, 2003
4
RU
Hi, I'd like to write a program, which can search the number of repeated words in text.

For example, to check how many times was every word repeated in text; or to check which words were repeated more than 5 times.

I heard there is some function 'grep', but I'm not sure how does it work.

@words = split(/\S/,$text);
foreach (@words) {
...
and check each word and make it as some variable, and check with all others - seems be too LONG and seems to EAT to many resources.

Anyone knows of an easier way?
 
I think you're best bet is to use a hash where the word is the key and the value the number of times it was repeated.

- Raenius

"Free will...is an illusion"
 
Take a look at thread219-711913 last post should give you an example of using a hash and counting them.



Blue [dragon]

If I wasn't Blue, I would just be a Dragon...
 
Hey guys, thanks for fast and useful answer :)

Worked perfectly. In case anyone would need it again :

print "Content-type: text/html\n\n";

$text = "Hello Bruce! It's so nice to meet you, pal, how've you been? I'm fine, and how are you?";

@words = split (/\s/,$text);
foreach (@words) {
$lowwords = lc($_);
$lowwords =~ s/[^a-zA-Z_0-9_:_-_']//;
$lowwords =~ s/[:_-_']$//;
$wordslist{$lowwords} += 1;
}

foreach (keys(%wordslist)) {
$push = "$wordslist{$_}|$_\n";
push (@finished,$push);
}

foreach $line (sort {$b <=> $a} @finished) {
($values,$keys) = split(/\|/,$line);
print &quot;$keys: $values<br>&quot;;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top