November 03, 2005, 02:31 PM
mgrackinCounting 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.SelphWhat 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
mgrackinVery slick! I will try this. Thanks!
November 03, 2005, 07:26 PM
Francis MarianiVery slick!
November 04, 2005, 11:03 AM
mgrackinHere 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
jbmuirand if there are multiple spaces between words? What then?
November 14, 2005, 12:50 PM
Francis MarianiThen 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