in reply to Counting the frequency of words in a string

I think the hardest part would be to define what makes "a word".

Given the definition was everything that doesn't match \W, this could do it:

my ( %wordcount, $relevancy ); for ( split /\W+/, $string ) # gets a list of all words { $wordcount{$_}++; } foreach my $word ( @list_of_words ) { $relevancy += $wordcount{$word}; }

I know there are smarter ways, for this can consume a lot of memory. Read on... =)

Cheers, Sören

Replies are listed 'Best First'.
Re: Counting the frequency of words in a string
by cez (Novice) on Dec 02, 2004 at 22:12 UTC
    how about:

    use Regexp::List, put the generated regex into $word_regex

    my $count = ($count = $text) =~ s/$word_regex//g;

    horrible?

      oh, i guess that could be just

      my $count = $text =~ s/$word_regex//g;

      (since destroying $text doesn't matter in my case)

        Or
        my $count = () = $text =~ /$word_regex/g;
        The parens make it list context, then you take the scalar context from that.