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

Last change on this file since 374 was 372, checked in by pinsard, 16 years ago

improvements of headers (alignments)

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