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