source: trunk/SRC/ToBeReviewed/STRING/getwrd.pro @ 232

Last change on this file since 232 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: 6.0 KB
Line 
1;+
2;
3; @file_comments
4; Return the n'th word from a text string.
5;
6; @categories
7; String
8;
9; @param TXTSTR {in}{required} {type=string or array of strings}
10; text string to extract from.
11; The first element is used if txt is an array.
12;
13; @param NTH {in}{required} {type=integer} {default=0}
14; word number to get (first = 0 = def)
15;
16; @param MTH {in}{optional} {type=integer}
17; optional last word number to get.
18;
19; @keyword LOCATION
20; = l.  Return word n string location.
21;
22; @keyword DELIMITER
23; = d. Set word delimiter (def = space & tab).
24;
25; @keyword LAST
26; means n is offset from last word.  So n=0 gives
27; last word, n=-1 gives next to last, ...
28; If n=-2 and m=0 then last 3 words are returned.
29;
30; @keyword NOTRIM
31; suppresses whitespace trimming on ends.
32;
33; @keyword NWORDS
34; = n. Returns number of words in string.
35;
36; @returns
37; wrd = returned word or words.
38;
39; @uses
40; getwrd_com
41;
42; @restrictions
43; If a NULL string is given (txt="") then the last string
44; given is used.  This saves finding the words again.
45; If m > n wrd will be a string of words from word n to
46; word m.  If no m is given wrd will be a single word.
47; n<0 returns text starting at word abs(n) to string end
48; If n is out of range then a null string is returned.
49; See also nwrds.
50;
51; @history
52;       Ray Sterner,  6 Jan, 1985.
53;       R. Sterner, Fall 1989 --- converted to SUN.
54;       R. Sterner, Jan 1990 --- added delimiter.
55;       R. Sterner, 18 Mar, 1990 --- added /LAST.
56;       R. Sterner, 31 Jan, 1991 --- added /NOTRIM.
57;       R. Sterner, 20 May, 1991 --- Added common and NULL string.
58;       R. Sterner, 13 Dec, 1992 --- Made tabs equivalent to spaces.
59;       R. Sterner,  4 Jan, 1993 --- Added NWORDS keyword.
60;       R. Sterner, 2001 Jan 15 --- Fixed to use first element if not a scalar.
61;       Johns Hopkins University Applied Physics Laboratory.
62;
63; Copyright (C) 1985, Johns Hopkins University/Applied Physics Laboratory
64; This software may be used, copied, or redistributed as long as it is not
65; sold and this copyright notice is reproduced on each copy made.  This
66; routine is provided as is without any express or implied warranties
67; whatsoever.  Other limitations apply as described in the file disclaimer.txt.
68;
69; @version
70; $Id$
71;
72;-
73;
74
75        FUNCTION getwrd, TXTSTR, NTH, MTH, help=hlp, location=ll,$
76           delimiter=delim, notrim=notrim, last=last, nwords=nwords
77
78        common getwrd_com, txtstr0, nwds, loc, len
79
80        if (n_params(0) lt 1) or keyword_set(hlp) then begin
81          print," Return the n'th word from a text string."
82          print,' wrd = getwrd(txt, n, [m])'
83          print,'   txt = text string to extract from.         in'
84          print,'     The first element is used if txt is an array.'
85          print,'   n = word number to get (first = 0 = def).  in'
86          print,'   m = optional last word number to get.      in'
87          print,'   wrd = returned word or words.              out'
88          print,' Keywords:'
89          print,'   LOCATION = l.  Return word n string location.'
90          print,'   DELIMITER = d. Set word delimiter (def = space & tab).'
91          print,'   /LAST means n is offset from last word.  So n=0 gives'
92          print,'     last word, n=-1 gives next to last, ...'
93          print,'     If n=-2 and m=0 then last 3 words are returned.'
94          print,'   /NOTRIM suppresses whitespace trimming on ends.'
95          print,'   NWORDS=n.  Returns number of words in string.'
96          print,'Note: If a NULL string is given (txt="") then the last string'
97          print,'      given is used.  This saves finding the words again.'
98          print,'      If m > n wrd will be a string of words from word n to'
99          print,'      word m.  If no m is given wrd will be a single word.'
100          print,'      n<0 returns text starting at word abs(n) to string end'
101          print,'      If n is out of range then a null string is returned.'
102          print,'      See also nwrds.'
103          return, -1
104        endif
105
106        if n_params(0) lt 2 then nth = 0                ; Def is first word.
107        IF N_PARAMS(0) LT 3 THEN MTH = NTH              ; Def is one word.
108
109        if strlen(txtstr[0]) gt 0 then begin
110          ddel = ' '                                    ; Def del is a space.
111          if n_elements(delim) ne 0 then ddel = delim   ; Use given delimiter.
112          TST = (byte(ddel))[0]                         ; Del to byte value.
113          tb = byte(txtstr[0])                          ; String to bytes.
114          if ddel eq ' ' then begin                     ; Check for tabs?
115            w = where(tb eq 9B, cnt)                    ; Yes.
116            if cnt gt 0 then tb[w] = 32B                ; Convert any to space.
117          endif
118          X = tb NE TST                                 ; Non-delchar (=words).
119          X = [0,X,0]                                   ; 0s at ends.
120
121          Y = (X-SHIFT(X,1)) EQ 1                       ; Diff=1: word start.
122          Z = WHERE(SHIFT(Y,-1) EQ 1)                   ; Word start locations.
123          Y2 = (X-SHIFT(X,-1)) EQ 1                     ; Diff=1: word end.
124          Z2 = WHERE(SHIFT(Y2,1) EQ 1)                  ; Word end locations.
125
126          txtstr0 = txtstr[0]                           ; Move string to common.
127          NWDS = long(TOTAL(Y))                         ; Number of words.
128          LOC = Z                                       ; Word start locations.
129          LEN = Z2 - Z - 1                              ; Word lengths.
130        endif else begin
131          if n_elements(nwds) eq 0 then begin           ; Check if first call.
132            print,' Error in getwrd: must give a '+$
133              'non-NULL string on the first call.'
134            return, -1                                  ; -1 = error flag.
135          endif
136        endelse
137
138        nwords = nwds                                   ; Set nwords
139
140        if keyword_set(last) then begin                 ; Offset from last.
141          lst = nwds - 1
142          in = lst + nth                                ; Nth word.
143          im = lst + mth                                ; Mth word.
144          if (in lt 0) and (im lt 0) then return, ''    ; Out of range.
145          in = in > 0                                   ; Smaller of in and im
146          im = im > 0                                   ;  to zero.
147          if (in gt lst) and (im gt lst) then return,'' ; Out of range.
148          in = in < lst                                 ; Larger of in and im
149          im = im < lst                                 ;  to be last.
150          ll = loc[in]                                  ; Nth word start.
151          return, strtrim(strmid(txtstr0,ll,loc[im]-loc[in]+len[im]), 2)
152        endif
153
154        N = ABS(NTH)                                    ; Allow nth<0.
155        IF N GT NWDS-1 THEN RETURN,''                   ; out of range, null.
156        ll = loc[n]                                     ; N'th word position.
157        IF NTH LT 0 THEN GOTO, NEG                      ; Handle nth<0.
158        IF MTH GT NWDS-1 THEN MTH = NWDS-1              ; Words to end.
159
160        if keyword_set(notrim) then begin
161          RETURN, STRMID(TXTSTR0,ll,LOC[MTH]-LOC[NTH]+LEN[MTH])
162        endif else begin
163          RETURN, strtrim(STRMID(TXTSTR0,ll,LOC[MTH]-LOC[NTH]+LEN[MTH]), 2)
164        endelse
165
166NEG:    if keyword_set(notrim) then begin
167          RETURN, STRMID(TXTSTR0,ll,9999)
168        endif else begin
169          RETURN, strtrim(STRMID(TXTSTR0,ll,9999), 2)
170        endelse
171
172        END
Note: See TracBrowser for help on using the repository browser.