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

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

change *.pro file properties (del eof-style, del executable, set keywords Id

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