# EXAMINE - count words

**URL:** <https://techcommunity.softwareag.com/t/examine-count-words/62099>\
**Category:** Forum\
**Tags:** Adabas-Natural, Natural, Natural-on-Mainframes\
**Created:** [March 29, 2007, 7:47pm UTC](https://techcommunity.softwareag.com/t/examine-count-words/62099 "2007-03-29T19:47:12Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guest](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/guest/32/75_2.png) [@Guest](https://techcommunity.softwareag.com/u/Guest)\
**Post date:** [March 29, 2007, 7:47pm UTC](https://techcommunity.softwareag.com/t/examine-count-words/62099/1 "2007-03-29T19:47:12Z")

</div>

Is a way to count words in for example #TEXT(A60) by EXAMINE ?

---

<div class="post-metadata">

**Author:** ![Jerome\_LeBlanc](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/jerome_leblanc/32/111_2.png) [@Jerome\_LeBlanc](https://techcommunity.softwareag.com/u/Jerome_LeBlanc)\
**Post date:** [March 29, 2007, 9:01pm UTC](https://techcommunity.softwareag.com/t/examine-count-words/62099/2 "2007-03-29T21:01:38Z")

</div>

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]](http://techcommunity.softwareag.com/ecosystem/documentation/natural/default.htm%5B/url%5D)

GIVING NUMBER is what you are looking for.

```auto
EXAMINE #A FOR 'ABC' GIVING NUMBER #N 
```

---

<div class="post-metadata">

**Author:** ![Don\_Kearney](https://avatars.discourse-cdn.com/v4/letter/d/3ec8ea/32.png) [@Don\_Kearney](https://techcommunity.softwareag.com/u/Don_Kearney)\
**Post date:** [March 29, 2007, 9:56pm UTC](https://techcommunity.softwareag.com/t/examine-count-words/62099/3 "2007-03-29T21:56:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ralph\_Zbrog](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/ralph_zbrog/32/78_2.png) [@Ralph\_Zbrog](https://techcommunity.softwareag.com/u/Ralph_Zbrog)\
**Post date:** [March 29, 2007, 10:59pm UTC](https://techcommunity.softwareag.com/t/examine-count-words/62099/4 "2007-03-29T22:59:55Z")

</div>

Depending on how you define “word” and the delimiters within #TEXT, you may find that the SEPARATE statement gives better results.

---

<div class="post-metadata">

**Author:** ![Matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Matthias1](https://techcommunity.softwareag.com/u/Matthias1)\
**Post date:** [March 30, 2007, 2:28pm UTC](https://techcommunity.softwareag.com/t/examine-count-words/62099/5 "2007-03-30T14:28:12Z")

</div>

I would solve the problem with multiple spaces the following way:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Klaus\_Sponagel](https://avatars.discourse-cdn.com/v4/letter/k/3bc359/32.png) [@Klaus\_Sponagel](https://techcommunity.softwareag.com/u/Klaus_Sponagel)\
**Post date:** [March 30, 2007, 3:28pm UTC](https://techcommunity.softwareag.com/t/examine-count-words/62099/6 "2007-03-30T15:28:40Z")

</div>

SEPARATE get rid of multiple spaces by using the LEFT JUSTIFIED option:

```auto

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
```

---

<div class="post-metadata">

**Author:** ![Matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Matthias1](https://techcommunity.softwareag.com/u/Matthias1)\
**Post date:** [March 30, 2007, 3:45pm UTC](https://techcommunity.softwareag.com/t/examine-count-words/62099/7 "2007-03-30T15:45:08Z")

</div>

> [@Klaus Sponagel](#):
>
> SEPARATE get rid of multiple spaces by using the LEFT JUSTIFIED option:

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

```auto
<'word1 word2 word3 ; word4'>
```

---

<div class="post-metadata">

**Author:** ![Klaus\_Sponagel](https://avatars.discourse-cdn.com/v4/letter/k/3bc359/32.png) [@Klaus\_Sponagel](https://techcommunity.softwareag.com/u/Klaus_Sponagel)\
**Post date:** [March 30, 2007, 4:38pm UTC](https://techcommunity.softwareag.com/t/examine-count-words/62099/8 "2007-03-30T16:38:35Z")

</div>

This is another approach that can also cope with the ‘spaced delimiters’:

```auto

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
```

---

<div class="post-metadata">

**Author:** ![Steve\_Robinson](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/steve_robinson/32/107_2.png) [@Steve\_Robinson](https://techcommunity.softwareag.com/u/Steve_Robinson)\
**Post date:** [March 30, 2007, 6:54pm UTC](https://techcommunity.softwareag.com/t/examine-count-words/62099/9 "2007-03-30T18:54:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Helmut\_Spichtinger](https://avatars.discourse-cdn.com/v4/letter/h/e0b2c6/32.png) [@Helmut\_Spichtinger](https://techcommunity.softwareag.com/u/Helmut_Spichtinger)\
**Post date:** [April 2, 2007, 8:38pm UTC](https://techcommunity.softwareag.com/t/examine-count-words/62099/10 "2007-04-02T20:38:16Z")

</div>

Just another proposal for solution if there are multiple spaces within a text string:

> [@](#):
>
> DEFINE DATA LOCAL  
> 1 #TEXT (A60) INIT \<’ word1 word2 word3 '\>  
> 1 #X (A1)  
> 1 #I (I4)  
> 1 #WORDCOUNT (I4)  
> 1 #LEN (I4)  
> END-DEFINE  
> EXAMINE #TEXT FOR ’ ’ GIVING LENGTH IN #LEN  
> IF #LEN \> 0  
> FOR #I = 1 TO #LEN  
> #X := SUBSTRING(#TEXT,#I,1)  
> IF #X = ’ ’ AND BREAK OF #X  
> ADD 1 TO #WORDCOUNT  
> END-IF  
> END-FOR  
> ADD 1 TO #WORDCOUNT  
> END-IF  
> DISPLAY #WORDCOUNT  
> END

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/techcommunity/original/3X/c/d/cd19967bb605d28a72d77eb83a1cae22f3df2eb8.svg) [@system](https://techcommunity.softwareag.com/u/system)\
**Post date:** [April 2, 2021, 2:41pm UTC](https://techcommunity.softwareag.com/t/examine-count-words/62099/11 "2021-04-02T14:41:08Z")

</div>


