Focal Point
[CLOSED] Character Count

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

July 26, 2006, 11:51 AM
slfmr
[CLOSED] Character Count
Is it possible to count the number of ~'s or any such character found in the a long string?


Thank you,
Stacy

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


Dev, SIT, UAT, Production:7.6.6
Dev Sandbox:7.6.11

Dev Studio - 7.6.6
July 26, 2006, 01:19 PM
susannah
Stacy, please edit your profile/signature so we know what version you're running.
In 5n, i can show you how to do it the hard way, using both the POSITion function and the SUBSTRing function.
The POSIT function will tell you where in the string a particular character or substring exists, with 0 meaning 'its not there'.
and SUBSTR will then allow you to create a new test string from the original one, starting at the position+1 of the search character.
Then repeat the process, searching the new substring until the POSIT function finally returns a 0. Count the number of times you have to do this in order to get a final 0.
That's the count of your search chars.




In Focus since 1979///7706m/5 ;wintel 2008/64;OAM security; Oracle db, ///MRE/BID
July 26, 2006, 03:05 PM
smiths
Here's a technique in DM that you can use. It can be particularly useful (when modified slightly) to perform character substitutions, such as replacing a single-quote with 2 consecutive single-quotes.

  
-* String to be searched
-SET &STRING = '~~~~12~~';

-* Character to count
-SET &SEARCHCHAR = '~';

-* Get the length of the string
-SET &STRING_LEN = &STRING.LENGTH;

-* Initialize the counter for the character being searched
-SET &CHARCOUNT = 0;

-* Initialize the loop index
-SET &IDX = 1;

-* Loop through the string a character at a time
-REPEAT :STRINGLOOP WHILE &IDX LE &STRING_LEN;

-* Get the next character in the string
-SET &CHARACTER = SUBSTR(&STRING_LEN, &STRING, &IDX, 
-                 &IDX, 1, 'A1');

-* If the current character matches the string being searched
-* increment the count by 1
-SET &CHARCOUNT = IF &CHARACTER EQ &SEARCHCHAR THEN &CHARCOUNT + 1
-                 ELSE &CHARCOUNT;

-* Advance to the next character
-SET &IDX = &IDX + 1;

-:STRINGLOOP

-* Display the character count
-TYPE Character '&SEARCHCHAR' occurs &CHARCOUNT times in string '&STRING'


Regards,
Sean

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


------------------------------------------------------------------------
PROD: WebFOCUS 7.6.2 on Unix AIX/Tomcat/Servlet Mode
TEST: WebFOCUS 7.6.2 on Unix AIX/Tomcat/Servlet Mode
July 26, 2006, 03:53 PM
drew billingslea
you might want to do an ARGLEN() then a STRIP() and then an ARGLEN() to see how many characters the STRIP() removed. (start_len - end_len)


WF 7.6.6 Linux, FOCUS 7.8 zOS
July 26, 2006, 04:23 PM
slfmr
Sorry about that Susanne. I am using version 534.

I went ahead and tried Smiths's suggestion and it worked perfectly.

Because I never know the length of the long string, I also used drew's suggestions for ARGLEN to count the total number of characters in the string. This will limit the amount of loops it will do.

The reason I did that because if not then I would generally make the string an A4096 when that is not the case. Here is my solution with all of your suggestions below:


TABLE FILE T_FILENAME
PRINT
COMPUTE LENGTH/I4 = ARGLEN(4096, STRNG_X, LENGTH); AS LENGTH
WHERE STRING_ID = &STRING_ID
ON TABLE HOLD AS HOLDLENGTH
END

TABLE FILE HOLDLENGTH
PRINT
LENGTH
ON TABLE HOLD AS HOLDL FORMAT ALPHA
END
-RUN

-READ HOLDL &LENGTH.A4
-SET &LENGTH = INT(&LENGTH);

TABLE FILE T_FILENAME
PRINT STRNG_X
WHERE STRING_ID = &STRING_ID
ON TABLE HOLD AS HOLDSTRING
END
-RUN

-*** THIS HELPED ME SPECIFY THE LENGTH EACH TIME BY COUNTING THE CHARACTERS AND HELP MAINTIAN PERFORMANCE INSTEAD OF ASSUMING 4096
-SET &FORM = '.A'||&LENGTH;

-READ HOLDSTRING &STRING.&FORM
-SET &STRING = &STRING;

-SET &SEARCHCHAR = '~';
-SET &STRING_LEN = &STRING.LENGTH;
-SET &CHARCOUNT = 0;
-SET &IDX = 1;

-REPEAT :STRINGLOOP WHILE &IDX LE &STRING_LEN;

-SET &CHARACTER = SUBSTR(&STRING_LEN, &STRING, &IDX,
- &IDX, 1, 'A1');

-SET &CHARCOUNT = IF &CHARACTER EQ &SEARCHCHAR THEN &CHARCOUNT + 1
- ELSE &CHARCOUNT;

-SET &IDX = &IDX + 1;
-:STRINGLOOP



Thank you so much to everyone that gave input. This has helped me very much!

---------------------------------------------
DEV: v534
UAT: v534
PROD: v534

Moving to 7.1 for Dev soon!


Dev, SIT, UAT, Production:7.6.6
Dev Sandbox:7.6.11

Dev Studio - 7.6.6
July 26, 2006, 04:58 PM
susannah
some pretty clever stuff here! very nice.




In Focus since 1979///7706m/5 ;wintel 2008/64;OAM security; Oracle db, ///MRE/BID
November 03, 2010, 06:23 AM
Ramkumar - Webfous
How about for Version 7n+ ... Is there a direct function for accomplishing this ?


Thanks,

Ramkumar.
WebFOCUS/Tableau
Webfocus 8 / 7.7.02
Unix, Windows
HTML/PDF/EXCEL/AHTML/XML/HTML5
November 03, 2010, 08:07 AM
GamP
No, there isn't.
But you can still use drew's method, in DM as well as TABLE. An example of both, combined into one is:
-DEFAULT &CHARACTER = 'O'

-SET &STRING = 'THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG';
-SET &_BEFORE  = ARGLEN(&STRING.LENGTH,&STRING,'I4');
-SET &STRIPPED = STRIP(&STRING.LENGTH,&STRING,&CHARACTER,'A&STRING.LENGTH');
-SET &_AFTER   = ARGLEN(&STRIPPED.LENGTH,&STRIPPED,'I4');
-SET &NRTIMES  = &_BEFORE - &_AFTER;

DEFINE FILE CAR
_BEFORE/I4   = ARGLEN(16,CAR,_BEFORE);
STRIPPED/A16 = STRIP(16,CAR,'&CHARACTER', STRIPPED);
_AFTER/I4    = ARGLEN(16,STRIPPED,_AFTER);
END
TABLE FILE CAR
"NUMBER OF TIMES &CHARACTER IS FOUND IN &STRING IS: &NRTIMES"
PRINT _BEFORE _AFTER
     COMPUTE NRTIMES/I2 = _BEFORE - _AFTER;
BY COUNTRY
BY CAR AS ''
END



GamP

- Using AS 8.2.01 on Windows 10 - IE11.
in Focus since 1988
November 16, 2010, 03:35 PM
TexasStingray
There is a user-written sub-routine on www.moonlightware.com that will accomplish this. click on the code library to get the list. It's called chacter count. It contains instructions, C source code, and a dll that you can use if you are running the reporting server on windows.

Hope this helps




Scott

Old thread but I thought I would add this for people looking in the future. This puts the character count logic into a re-usable function.

DEFINE FUNCTION COUNT_CHARS DESCRIPTION 'Returns the number of times a character occurs in a string.'(CHAR/A1,STRING/A4000V)
_BEFORE/I4 = ARGLEN(4000,STRING,'I4');
STRIPPED/A4000V = STRIP(4000,STRING,CHAR,'A4000V');
_AFTER/I4 = ARGLEN(4000,STRIPPED,'I4');
COUNT_CHARS/I4 = _BEFORE - _AFTER;
END



WF: WebFocus 8.0.02
InfoAssist, Dev Studio, Magnify, Portal/Dashboards, Mobile Favs
Data: DB2
OS: Windows
Output: Multiple