Focal Point
[SOLVED] Count blanks in a string

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

September 25, 2008, 02:42 PM
Tomsweb
[SOLVED] Count blanks in a string
I have been trying to count the number of blank
characters there are in each string below:

Peter Piper picked a peck of pickled peppers
A peck of pickled peppers Peter Piper picked.
If Peter Piper picked a peck of pickled peppers,
Where's the peck of pickled peppers Peter Piper picked?

I pine for the old mainframe days when you could use various REXX functions (POS, WORDPOS) to count the # of words etc., in a string. [Oh, and the early Elton John albums were
awesome too!]

So far I have used CTRAN to translate the blanks' to '*', but I am stumped on how to accumulate the # of blanks in a string.

Any ideas? Idea

Thanks,

This message has been edited. Last edited by: Kerry,


Tomsweb
WebFOCUS 8.1.05M, 8.2.x
APP Studio, Developer Studio, InfoAssist, Dashboards, charts & reports
Apache Tomcat/8.0.36
September 25, 2008, 02:50 PM
Francis Mariani
DEFINE FILE CAR
INSTRLEN/D4 = ARGLEN(24, MODEL, 'D4');
OUTSTR/A24 = STRIP(24, MODEL, ' ', 'A24');
OUTSTRLEN/D4 = ARGLEN(24, OUTSTR, 'D4');
NBR_BLANKS/D4 = INSTRLEN - OUTSTRLEN;
END

TABLE FILE CAR
PRINT
MODEL
INSTRLEN

OUTSTR
OUTSTRLEN

NBR_BLANKS
END



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
September 25, 2008, 03:22 PM
Francis Mariani
I imagine you will see what the program is doing:

1) Determine the length of the string
2) Strip the blanks out of the string
3) Determine the length of the string without the blanks
4) Determine the number of blanks in the string by subtracting the length of the string without the blanks from the length of the string


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
September 25, 2008, 04:03 PM
Tomsweb
Thanks very much!


Tomsweb
WebFOCUS 8.1.05M, 8.2.x
APP Studio, Developer Studio, InfoAssist, Dashboards, charts & reports
Apache Tomcat/8.0.36
September 25, 2008, 04:07 PM
TexasStingray
Many Many years back. I had a simular situation where we needed to know how many times a certain character occurred in a field. We ended up creating our own userwritten subroutine. O the power of WebFOCUS/FOCUS.




Scott

frankie martini, that's so delightfully clever...




In Focus since 1979///7706m/5 ;wintel 2008/64;OAM security; Oracle db, ///MRE/BID
Perhaps all WebFOCUS developers think alike. Roll Eyes

I thought, this is simple, I'll add to the post, and found Francis pipped me at the post by a couple of posts.

A great concept to keep in mind. The field will always be the same length. STRIPping the spaces effectively left justifies all text or right justifies all spaces. Good One


Waz...

Prod:WebFOCUS 7.6.10/8.1.04Upgrade:WebFOCUS 8.2.07OS:LinuxOutputs:HTML, PDF, Excel, PPT
In Focus since 1984
Pity the lost knowledge of an old programmer!

It is a bit surprising that there isn't a supplied function to do this. We can easily convert my DEFINE code into a DEFINE FUNCTION so that it can be called from any program, passing only the character to be counted. One thing to remember is that if the character to be counted is not a space, then the existing spaces have to be CTRANed to something else before CTRANing the character to be counted!


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
Francis, thats a great idea.

A whitespace count, or at lease a define function with a parameter to choose space or whitespace.


Waz...

Prod:WebFOCUS 7.6.10/8.1.04Upgrade:WebFOCUS 8.2.07OS:LinuxOutputs:HTML, PDF, Excel, PPT
In Focus since 1984
Pity the lost knowledge of an old programmer!

2 alternative solutions 1 using POSIT and STRIP the other ARGLEN and STRIP

COMPUTE NBR_BLANKS1/I5 = 11 - (POSIT(STRIP(10, COUNTRY, ' ', 'A10'), 10, ' ', 1, 'I5'));
COMPUTE NBR_BLANKS2/D4 = 10 - (ARGLEN(10, STRIP(10, COUNTRY, ' ', 'A10'), 'D4'));
TexasStingray:

Could please post your user-written subroutine to count spaces / characters in a string, field or file ?

We’d like to see how you accomplished this, and how it can be used efficiently in actual code, on large files.

Thanks


WF 7.6.4 & 5.3
Charles Lee
It is available for download here

http://www.moonlightware.com

click on the Code Library.

you need FLASH 9 or higher to that website.




Scott

You say "O the power of WebFOCUS/FOCUS"

Is there a way to write a similar WebFOCUS routine without using external calls to another language ?

Thanks for the link !


WF 7.6.4 & 5.3
Charles Lee