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