Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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]
[Corion]: Lady_Aleena: Mostly, it's just housekeeping, but it inspires me about things I could do with it, so that's good ;)
[Lady_Aleena]: By the way, I stopped by to let someone know I'm still alive, though I haven't found anything to write in perl for a while now.
[Lady_Aleena]: I need inspiration.
[Discipulus]: hello there: Lady_Aleena for inspiration random browse my homenode.. ;=)
[Lady_Aleena]: Discipulus, Hello there. How are you?

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2017-09-24 18:18 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (274 votes). Check out past polls.