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