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

Last change on this file since 206 was 142, checked in by navarro, 18 years ago

english and nicer header (2a)

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