Gossamer Forum
Quote Reply
Word count
Hi,

Is there an easy way to print the number of words in certain fields?

ex: <%word_count($Description+$Story%>

Any suggestions will be appreciated

Klaus

http://www.ameinfo.com
Quote Reply
Re: [klauslovgreen] Word count In reply to
Well, there is guaranteed to be a better/faster way to do this, but you could try;

Code:
sub {
my $word = shift;
my $count;
my @count = split(" ",$word);
foreach (@count) {
$count++;
}
return $count;
}

That will slice up the description passed in via the tag, and then a foreach counts the words.

Hopefully that will work Smile

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [klauslovgreen] Word count In reply to
Code:
sub {
my $count = 0;
$count += length for @_;
return $count;
}

Comma delimit the tags instaeed of using + eg..

<%word_count($Description, $story)%>

Last edited by:

Paul: Mar 22, 2003, 1:34 AM
Quote Reply
Re: [klauslovgreen] Word count In reply to
Whoops sorry I just realised to said word count.

Try:

Code:
sub {
my $count = 0;
for (@_) {
my @words = split /\b/;
$count += scalar @words;
}
return $count;
}

Last edited by:

Paul: Mar 22, 2003, 1:40 AM
Quote Reply
Re: [Paul] Word count In reply to
Thanks guys

Klaus

http://www.ameinfo.com
Quote Reply
Re: [klauslovgreen] Word count In reply to
Probably a shorter code:
Code:
sub {
return scalar(split /\s+/, shift . ' ' . shift);
}

Well, not tested, so not sure if will work. Give it a try.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...

Last edited by:

webmaster33: Mar 22, 2003, 8:09 PM
Quote Reply
Re: [webmaster33] Word count In reply to
You know the saying "Quality not quantity" Tongue

You are just splitting on spaces, I'm splitting on word boundaries.
Quote Reply
Re: [Paul] Word count In reply to
I don't bother your sharp words... It's very weak try to discredit my shorter solution.
Anyway, both of yours & mine solution should work.

Also the change to use word boundary is easy:
Code:
sub {
return scalar(split /\b/, shift . ' ' . shift);
}

Any more problems? Cool

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Word count In reply to
The first line of my post was me making a joke :)

The second was true.