Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Count number of words on a web page?

by JSchmitz (Canon)
on Feb 09, 2010 at 13:39 UTC ( #822190=note: print w/replies, xml ) Need Help??

in reply to Count number of words on a web page?

#!/usr/bin/perl $/ = ""; $* = 1; while (<>) { #s/-\n//g; tr/A-Z/a-z/; @words = split(/\W*\s+\W*/, $_); # split into words foreach $word (@words) { $wordcount{$word}++; # count the words } } foreach $word (sort keys(%wordcount)) { printf "%8d\t\t%s\n", $wordcount{$word}, $word; }

Hope that helps


Replies are listed 'Best First'.
Re^2: Count number of words on a web page?
by cdarke (Prior) on Feb 09, 2010 at 16:26 UTC
    Use word boundaries (\b).
    $_ ='The,quick,brown;foxy. Does a lot,of:jumping!'; my @words = split(/\W*\s+\W*/, $_); # split into words print 'Number of words: '.@words."\n";
    gives 4 words. However
    my @words = split(/\b\W*/, $_); # split into words
    gives 9 words.
Re^2: Count number of words on a web page?
by jdlev (Scribe) on Feb 09, 2010 at 13:49 UTC
    I love it when a program comes together - jdhannibal

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://822190]
[Lady_Aleena]: marioroy, it is used in 24 other modules I wrote.
[Lady_Aleena]: GUH! I wrote a script better than I thought! 8)
[marioroy]: Wow. It's cool seeing Github coments "Update, Create Wild_hairs, Create alternate- PurpleOnGreen..., Then a location, Create Aleenia. Amazing, really. Thank you for sharing.

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2017-05-29 09:07 GMT
Find Nodes?
    Voting Booth?