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

Last change on this file since 358 was 325, checked in by pinsard, 17 years ago

modification of some headers (+some corrections) to prepare usage of the new idldoc

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