Is a way to count words in for example #TEXT(A60) by EXAMINE ?
Do you have a Natural manual? If not you can use the SAG documentation at [url]http://techcommunity.softwareag.com/ecosystem/documentation/natural/default.htm[/url]
GIVING NUMBER is what you are looking for.
EXAMINE #A FOR 'ABC' GIVING NUMBER #N
EXAMINE #TEXT FOR ’ ’ GIVING NUMBER #N
#N + 1 will give the number of words as long as each word has only 1 space between each word.
Depending on how you define “word” and the delimiters within #TEXT, you may find that the SEPARATE statement gives better results.
I would solve the problem with multiple spaces the following way:
define data local
1 #text (a60) init <'word1 word2 word3;word4'>
1 #i4 (I4)
1 #wordcount (I4)
1 #word-delimiters(A1/1:2) init <' ',';'>
1 #old_char (A1)
end-define
reset #old_char
for #i4 = 1 to 60
if #old_char = #word-delimiters(*)
and not substr(#text,#i4,1) = #word-delimiters(*)
add 1 to #wordcount
end-if
#old_char := substr(#text,#i4,1)
end-for
display #wordcount
end
EXAMINE is much more simple but has the disadvantage of counting multiple spaces.
SEPARATE #TEXT INTO #TAB() GIVING NUMBER also counts multiple spaces. To avoid that, you can do a compress of #TAB() afterwards and then count the spaces with EXAMINE.
SEPARATE get rid of multiple spaces by using the LEFT JUSTIFIED option:
0010 DEFINE DATA LOCAL
0020 1 #TEXT (A60) INIT <'word1 word2 word3;word4'>
0030 1 #A(A20/60)
0040 1 #WORDCOUNT (I4)
0050 1 #WORD-DELIMITERS(A3) INIT <' ,;'>
0060 END-DEFINE
0070 SEPARATE #TEXT LEFT INTO #A(*)
0080 WITH DELIMITERS #WORD-DELIMITERS
0090 GIVING NUMBER #WORDCOUNT
0100 WRITE #TEXT / #WORDCOUNT
0110 END
Thanks for that hint. I didn’t know that. The only thing in difference to my solution is the count of words in the following string
<'word1 word2 word3 ; word4'>
This is another approach that can also cope with the ‘spaced delimiters’:
0010 DEFINE DATA LOCAL
0020 1 #TEXT (A60) INIT <'word1 word2 word3; word4'>
0030 1 #A(A20/60)
0040 1 #WORDCOUNT (I4)
0051 1 #TR(A2/2) INIT <'; ', ', '>
0060 END-DEFINE
0061 EXAMINE #TEXT TRANSLATE USING #TR(*)
0070 SEPARATE #TEXT LEFT INTO #A(*)
0080 WITH DELIMITERS ' '
0090 GIVING NUMBER #WORDCOUNT
0100 WRITE #TEXT / #WORDCOUNT
0110 END
Lots of interesting discussion.
HOWEVER, no follow up post from mun1ek. What are the actual characteristics of the problem you are trying to solve? Are you only concerned with blanks as delimiters, or are you also concerned with commas, comma-blank, blank-blank, etc.
steve
Just another proposal for solution if there are multiple spaces within a text string: