
japhy
Enthusiast
Feb 27, 2000, 6:12 AM
Post #3 of 4
(1685 views)
|
Re: snippett wanted for word count
[In reply to]
|
Can't Post
|
|
It depends on what you call a "word". Here's something that you can use: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> # get_words EXPR, WCHARS, MAXWORDS sub get_words { my ($string, $wchr, $max) = @_; $max | |= 0; my @words; eval << "END OF REGEX"; local \$_ = \$string; for ( my \$i = 0; ($max < 1 or \$i < $max) and /\\G([$wchr]+[^$wchr]*)/g; \$i++) { push \@words, \$1; } END OF REGEX return wantarray ? @words : join "", @words; } </pre><HR></BLOCKQUOTE> It's used as follows: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> $text = "What's up with you?"; $short = get_words($text, "a-zA-Z", 3); # What's up $short = get_words($text, "-'a-zA-Z", 3); # What's up with @chunks = get_words($text, "a-zA-Z", </pre><HR></BLOCKQUOTE> The second argument is the character class that "words" are made up of. If the third argument is not passed, it will break the string up as many times as needed. Play with the function a bit. [This message has been edited by japhy (edited 02-27-2000).]
|