1 | ;+ |
---|
2 | ; |
---|
3 | ; @file_comments |
---|
4 | ; Return the number of words in the given text string. |
---|
5 | ; |
---|
6 | ; @categories |
---|
7 | ; |
---|
8 | ; @param TXTSTR |
---|
9 | ; = text string to examine. |
---|
10 | ; |
---|
11 | ; @keyword DELIMITER |
---|
12 | ; = d. Set delimiter character (def = space). |
---|
13 | ; |
---|
14 | ; @keyword HELP |
---|
15 | ; |
---|
16 | ; @returns |
---|
17 | ; n = number of words found. |
---|
18 | ; |
---|
19 | ; @restrictions |
---|
20 | ; See also getwrd. |
---|
21 | ; |
---|
22 | ; @history |
---|
23 | ; R. Sterner, 7 Feb, 1985. |
---|
24 | ; Johns Hopkins University Applied Physics Laboratory. |
---|
25 | ; RES 4 Sep, 1989 --- converted to SUN. |
---|
26 | ; |
---|
27 | ; Copyright (C) 1985, Johns Hopkins University/Applied Physics Laboratory |
---|
28 | ; This software may be used, copied, or redistributed as long as it is not |
---|
29 | ; sold and this copyright notice is reproduced on each copy made. This |
---|
30 | ; routine is provided as is without any express or implied warranties |
---|
31 | ; whatsoever. Other limitations apply as described in the file disclaimer.txt. |
---|
32 | ; |
---|
33 | ; @version |
---|
34 | ; $Id$ |
---|
35 | ; |
---|
36 | ;- |
---|
37 | ; |
---|
38 | FUNCTION nwrds, txtstr, HELP=hlp, DELIMITER=delim |
---|
39 | ; |
---|
40 | compile_opt idl2, strictarrsubs |
---|
41 | ; |
---|
42 | |
---|
43 | if (n_params(0) lt 1) or keyword_set(hlp) then begin |
---|
44 | print,' Return the number of words in the given text string.' |
---|
45 | print,' n = nwrds(txt)' |
---|
46 | print,' txt = text string to examine. in' |
---|
47 | print,' n = number of words found. out' |
---|
48 | print,' Keywords:' |
---|
49 | print,' DELIMITER = d. Set delimiter character (def = space).' |
---|
50 | print,' Notes: See also getwrd.' |
---|
51 | return, -1 |
---|
52 | endif |
---|
53 | |
---|
54 | if strlen(txtstr) eq 0 then return,0 ; A null string has 0 words. |
---|
55 | ddel = ' ' ; Default word delimiter is a space. |
---|
56 | if n_elements(delim) ne 0 then ddel = delim ; Use given word delimiter. |
---|
57 | tst = (byte(ddel))[0] ; Delimiter as a byte value. |
---|
58 | tb = byte(txtstr) ; String to bytes. |
---|
59 | if ddel eq ' ' then begin ; Check for tabs? |
---|
60 | w = where(tb eq 9B, cnt) ; Yes. |
---|
61 | if cnt gt 0 then tb[w] = 32B ; Convert any to space. |
---|
62 | endif |
---|
63 | x = tb ne tst ; Locate words. |
---|
64 | x = [0,x,0] ; Pad ends with delimiters. |
---|
65 | |
---|
66 | y = (x-shift(x,1)) eq 1 ; Look for word beginnings. |
---|
67 | |
---|
68 | n = fix(total(y)) ; Count word beginnings. |
---|
69 | |
---|
70 | return, n |
---|
71 | |
---|
72 | end |
---|