source: trunk/SRC/ToBeReviewed/STRING/nwrds.pro @ 236

Last change on this file since 236 was 232, checked in by pinsard, 17 years ago

improvements/corrections of some *.pro headers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.1 KB
Line 
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 
39        function nwrds,txtstr, help=hlp, delimiter=delim
40;
41  compile_opt idl2, strictarrsubs
42;
43 
44        if (n_params(0) lt 1) or keyword_set(hlp) then begin
45          print,' Return the number of words in the given text string.'
46          print,' n = nwrds(txt)'
47          print,'   txt = text string to examine.             in'
48          print,'   n = number of words found.                out'
49          print,' Keywords:'
50          print,'   DELIMITER = d.  Set delimiter character (def = space).'
51          print,' Notes: See also getwrd.'
52          return, -1
53        endif
54 
55        if strlen(txtstr) eq 0 then return,0    ; A null string has 0 words.
56        ddel = ' '                      ; Default word delimiter is a space.
57        if n_elements(delim) ne 0 then ddel = delim ; Use given word delimiter.
58        tst = (byte(ddel))[0]                   ; Delimiter as a byte value.
59        tb = byte(txtstr)                             ; String to bytes.
60        if ddel eq ' ' then begin                     ; Check for tabs?
61          w = where(tb eq 9B, cnt)                    ; Yes.
62          if cnt gt 0 then tb[w] = 32B                ; Convert any to space.
63        endif
64        x = tb ne tst                           ; Locate words.
65        x = [0,x,0]                             ; Pad ends with delimiters.
66 
67        y = (x-shift(x,1)) eq 1                 ; Look for word beginnings.
68 
69        n = fix(total(y))                       ; Count word beginnings.
70 
71        return, n
72 
73        end
Note: See TracBrowser for help on using the repository browser.