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

Last change on this file since 325 was 325, checked in by pinsard, 16 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: 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        FUNCTION getwrd, txtstr, nth, mth, HELP=hlp, LOCATION=ll,$
74           DELIMITER=delim, NOTRIM=notrim, LAST=last, NWORDS=nwords
75
76        common getwrd_com, txtstr0, nwds, loc, len
77
78        if (n_params(0) lt 1) or keyword_set(hlp) then begin
79          print," Return the n'th word from a text string."
80          print,' wrd = getwrd(txt, n, [m])'
81          print,'   txt = text string to extract from.         in'
82          print,'     The first element is used if txt is an array.'
83          print,'   n = word number to get (first = 0 = def).  in'
84          print,'   m = optional last word number to get.      in'
85          print,'   wrd = returned word or words.              out'
86          print,' Keywords:'
87          print,'   LOCATION = l.  Return word n string location.'
88          print,'   DELIMITER = d. Set word delimiter (def = space & tab).'
89          print,'   /LAST means n is offset from last word.  So n=0 gives'
90          print,'     last word, n=-1 gives next to last, ...'
91          print,'     If n=-2 and m=0 then last 3 words are returned.'
92          print,'   /NOTRIM suppresses whitespace trimming on ends.'
93          print,'   NWORDS=n.  Returns number of words in string.'
94          print,'Note: If a NULL string is given (txt="") then the last string'
95          print,'      given is used.  This saves finding the words again.'
96          print,'      If m > n wrd will be a string of words from word n to'
97          print,'      word m.  If no m is given wrd will be a single word.'
98          print,'      n<0 returns text starting at word abs(n) to string end'
99          print,'      If n is out of range then a null string is returned.'
100          print,'      See also nwrds.'
101          return, -1
102        endif
103
104        if n_params(0) lt 2 then nth = 0                ; Def is first word.
105        IF N_PARAMS(0) LT 3 THEN MTH = NTH              ; Def is one word.
106
107        if strlen(txtstr[0]) gt 0 then begin
108          ddel = ' '                                    ; Def del is a space.
109          if n_elements(delim) ne 0 then ddel = delim   ; Use given delimiter.
110          TST = (byte(ddel))[0]                         ; Del to byte value.
111          tb = byte(txtstr[0])                          ; String to bytes.
112          if ddel eq ' ' then begin                     ; Check for tabs?
113            w = where(tb eq 9B, cnt)                    ; Yes.
114            if cnt gt 0 then tb[w] = 32B                ; Convert any to space.
115          endif
116          X = tb NE TST                                 ; Non-delchar (=words).
117          X = [0,X,0]                                   ; 0s at ends.
118
119          Y = (X-SHIFT(X,1)) EQ 1                       ; Diff=1: word start.
120          Z = WHERE(SHIFT(Y,-1) EQ 1)                   ; Word start locations.
121          Y2 = (X-SHIFT(X,-1)) EQ 1                     ; Diff=1: word end.
122          Z2 = WHERE(SHIFT(Y2,1) EQ 1)                  ; Word end locations.
123
124          txtstr0 = txtstr[0]                           ; Move string to common.
125          NWDS = long(TOTAL(Y))                         ; Number of words.
126          LOC = Z                                       ; Word start locations.
127          LEN = Z2 - Z - 1                              ; Word lengths.
128        endif else begin
129          if n_elements(nwds) eq 0 then begin           ; Check if first call.
130            print,' Error in getwrd: must give a '+$
131              'non-NULL string on the first call.'
132            return, -1                                  ; -1 = error flag.
133          endif
134        endelse
135
136        nwords = nwds                                   ; Set nwords
137
138        if keyword_set(last) then begin                 ; Offset from last.
139          lst = nwds - 1
140          in = lst + nth                                ; Nth word.
141          im = lst + mth                                ; Mth word.
142          if (in lt 0) and (im lt 0) then return, ''    ; Out of range.
143          in = in > 0                                   ; Smaller of in and im
144          im = im > 0                                   ;  to zero.
145          if (in gt lst) and (im gt lst) then return,'' ; Out of range.
146          in = in < lst                                 ; Larger of in and im
147          im = im < lst                                 ;  to be last.
148          ll = loc[in]                                  ; Nth word start.
149          return, strtrim(strmid(txtstr0,ll,loc[im]-loc[in]+len[im]), 2)
150        endif
151
152        N = ABS(NTH)                                    ; Allow nth<0.
153        IF N GT NWDS-1 THEN RETURN,''                   ; out of range, null.
154        ll = loc[n]                                     ; N'th word position.
155        IF NTH LT 0 THEN GOTO, NEG                      ; Handle nth<0.
156        IF MTH GT NWDS-1 THEN MTH = NWDS-1              ; Words to end.
157
158        if keyword_set(notrim) then begin
159          RETURN, STRMID(TXTSTR0,ll,LOC[MTH]-LOC[NTH]+LEN[MTH])
160        endif else begin
161          RETURN, strtrim(STRMID(TXTSTR0,ll,LOC[MTH]-LOC[NTH]+LEN[MTH]), 2)
162        endelse
163
164NEG:    if keyword_set(notrim) then begin
165          RETURN, STRMID(TXTSTR0,ll,9999)
166        endif else begin
167          RETURN, strtrim(STRMID(TXTSTR0,ll,9999), 2)
168        endelse
169
170        END
Note: See TracBrowser for help on using the repository browser.