Focal Point
Counting the words in a string

This topic can be found at:
https://forums.informationbuilders.com/eve/forums/a/tpc/f/7971057331/m/1321019331

November 03, 2005, 02:31 PM
mgrackin
Counting the words in a string
Has anyone developed a technique to count the number of words in a string? A blank is the word separator. I would like to create a report that counts the number of words in an A254 field.
November 03, 2005, 03:23 PM
N.Selph
What about using ARGLEN to find out how many characters are in the string, Doing a STRIP to remove all blank spaces, and then doing an ARGLEN again on the result? The difference (plus one) should be the number of words you had.
November 03, 2005, 03:41 PM
mgrackin
Very slick! I will try this. Thanks!
November 03, 2005, 07:26 PM
Francis Mariani
Very slick!
November 04, 2005, 11:03 AM
mgrackin
Here is the code for the DEFINE FUNCTION I created using your suggested technique.

DEFINE FUNCTION WORDCNT(THESRCE/A254)
ORG_LEN/I3=ARGLEN(254,THESRCE,'I3');
SHRTVER/A254=STRIP(254,THESRCE,' ','A254');
NEW_LEN/I3=ARGLEN(254,SHRTVER,'I3');
WORDCNT/I3=ORG_LEN - NEW_LEN + 1;
END

TABLE FILE MYFILE
PRINT DESCRIPTION
COMPUTE DESCWORDS/I3=WORDCNT(DESCRIPTION);
END

This works great! Things like a directory structure (c:\inetpub\wwwroot\baseapps) get counted as one word but for my purpoases this is perfect.

Thanks again!

(I'm kind of liking this DEFINE FUNCTION thing.)
November 14, 2005, 12:31 PM
jbmuir
and if there are multiple spaces between words? What then?


WF 7.1.6 moving to WF 7.7, Solaris 10, HTML,PDF,XL
November 14, 2005, 12:50 PM
Francis Mariani
Then you could incorporate the SQUEEZ function (Reduce Multiple Spaces to a Single Space)


Francis


Give me code, or give me retirement. In FOCUS since 1991

Production: WF 7.7.05M, Dev Studio, BID, MRE, WebSphere, DB2 / Test: WF 8.1.05M, App Studio, BI Portal, Report Caster, jQuery, HighCharts, Apache Tomcat, MS SQL Server