Re: function to convert string to array??? [message #3906 is a reply to message #3843] |
Wed, 29 March 1995 00:00  |
cox
Messages: 1 Registered: March 1995
|
Junior Member |
|
|
Here is my routine which converts a string of space or comma-delimited items into a
string array. It doesn't assume what the output should be, so it doesn't convert to
FLOAT of INTEGER. It avoids the use of loops and makes as much use as possible of
built-in IDL functions. It should work with ASCII and non-ASCII character sets.
;+
; NAME:
; parse_string
;
; PURPOSE:
; This procedure was written to parse a string for substrings
; separated by either spaces or commas.
;
; CATEGORY:
;
; CALLING SEQUENCE:
; substrings = parse_string(strng)
;
; POSITIONAL PARAMETERS:
; <INPUT>
; strng STRING A string containing multiple sub-
; strings separated by commas or by
; spaces.
; <INPUT/OUTPUT>
; None.
;
; <OUTPUT>
; None.
;
; KEYWORD PARAMETERS:
; None.
;
; MODIFICATION HISTORY:
; Written by: R.J. Cox, CPI, 1993
; Modified by: R.J. Cox, CPI, 2 Feb 1995
; Extensively recoded to make more use of
; built in IDL commands and to remove loops.
;-
;*********************************************************** **********
;
function parse_string, strng
;
;*********************************************************** **********
Separators = BYTE(', ')
ByteStrng = BYTE(strng)
;------------------------------------------
; replace commas by single spaces
;------------------------------------------
CommaPos = where(ByteStrng eq Separators(0), count)
if (count gt 0) then ByteStrng(CommaPos) = Separators(1)
;------------------------------------------
; Replace all whitespace by a single space
;------------------------------------------
strng_ = STRCOMPRESS(STRING(ByteStrng))
ByteStrng = BYTE(Strng_)
SpacePos = where(ByteStrng eq Separators(1), count)
count = count + 1
SubString = strarr(count)
SpacePos = [-1,SpacePos,strlen(strng_)+1]
for i = 0, count - 1 do begin
SubString(i) = strmid(strng_, SpacePos(i)+1, SpacePos(i+1)-SpacePos(i)-1)
endfor
return, SubString
end
-------------------------
Robin Cox,
Computational Physics, Inc.
|
|
|