;+ ; NAME: ; TagArray ; PURPOSE: ; (First check whether the IDL functions strjoin and/or ; STRSPLIT can do what you need) ; Combine string arrays into single string separated by ; separator character, or v.v. ; CATEGORY: ; Toolbox: generic ; CALLING SEQUENCE: FUNCTION TagArray, tag_in, sep, nonull=nonull, split=split ; INPUTS: ; tag scalar, array; type: string ; if scalar, or /split keyword set: ; the string is decomposed into elements separated by the ; 'sep' character(s). The result is returned as a string array ; with one dimension more than the input array: the leading ; dimension is the number of tags found (see RESTRICTIONS). ; if array: the elements in the array are ; concatenated, separated by the 'sep' character ; OPTIONAL INPUT PARAMETERS: ; sep scalar, or array; type: 1-char string: default is a scalar and depends on OS ; if /split is used then 'sep' can be an array of 1-char separator strings ; /nonull if extracting tags, discard null-string tags ; /split if 'tag' is an array that needs to be decomposed then ; split needs to be set (by default, arrays are ; concatenated; a scalar is always decomposed, even ; if /split is not set). ; OUTPUTS: ; R either a string array of tags, or a scalar string ; of concatenated tags. ; INCLUDE: @compile_opt.pro ; On error, return to caller ; CALLS: ; InitVar, IsType ; RESTRICTIONS: ; If keyword /split is set than the first elements in the 'tag' array is analyzed ; for the presence of separators. The result is applied to all strings. So all ; strings in 'tag' must have the same structure: same length and separators in the ; same positions. ; SIDE EFFECTS: ; Note that an array[1] is treated differently from a scalar. ; An array[1] is returned unmodified as a scalar ; it is NOT decomposed if it contains 'sep' characters. ; EXAMPLE: ; t = tagarray('1998:337:23:53.000',[':','.'] ; results in: ; t =['1998','337','23','53','000'] ; PROCEDURE: ; The default separator is a comma on 'Win32', a colon on 'linux' ; On other OS it is a colon (as on linux). ; MODIFICATION HISTORY: ; NOV-1999, Paul Hick (UCSD/CASS; pphick@ucsd.edu) ;- InitVar, nonull, /key InitVar, split , /key IF IsType(sep, /undefined) THEN BEGIN CASE !version.os OF 'Win32': sep = ',' 'linux': sep = ':' ELSE : sep = ':' ENDCASE ENDIF stag = size(tag_in) nstr = stag[stag[0]+2] split = split OR (nstr EQ 1 AND stag[0] EQ 0) IF split THEN BEGIN ; Decompose tag_b = byte(tag_in) ; Array with one extra leading dimension sb = size(tag_b) nb = sb[1] ; # chars in string tag_b = reform(tag_b, nb, nstr, /overwrite) ; Use first string to locate separators ptags = -1 FOR i=0,n_elements(sep)-1 DO BEGIN j = where( (byte(sep[i]))[0] EQ tag_b[*,0] ) IF j[0] NE -1 THEN ptags = [ptags,j] ENDFOR ptags = [ptags,nb] ptags = ptags[sort(ptags)] ntags = n_elements(ptags) ltags = ptags[0] ltags = ptags-shift(ptags,1)-1 ntag_out = ntags-1-nonull*total(ltags EQ 0) tag_out = bytarr(max(ltags),ntag_out,nstr) j = -1 FOR i=1,ntags-1 DO BEGIN IF ltags[i] GT 0 THEN BEGIN j += 1 tag_out[0:ltags[i]-1,j,*] = tag_b[ptags[i-1]+1:ptags[i]-1,*] ENDIF ELSE IF NOT nonull THEN $ j += 1 ENDFOR tag_out = string(tag_out) IF stag[0] GT 1 THEN tag_out = reform(tag_out,[ntag_out,stag[1:stag[0]]],/overwrite) ELSE tag_out = [tag_out] ENDIF ELSE BEGIN ; Concatenate tag_in = reform([tag_in], stag[1], nstr/stag[1], /overwrite) tag_out = tag_in[0,*] FOR i=1,stag[1]-1 DO tag_out = tag_out+sep+tag_in[i,*] IF stag[0] EQ 1 THEN tag_out = tag_out[0] ELSE tag_out = reform(tag_out, stag[2:stag[0]], /overwrite) ENDELSE RETURN, tag_out & END