Re: function to convert string to array??? [message #3816] |
Fri, 17 March 1995 15:06  |
metcalf
Messages: 3 Registered: May 1991
|
Junior Member |
|
|
In article <3kb4ud$jv0@senator-bedfellow.MIT.EDU>, davis@space.mit.edu writes:
|> In article <3k89l3$2ap@news.csus.edu>, Chris McCarthy <chris@quark.sfsu.edu> writes:
|> : davis@space.mit.edu wrote:
|> : >
|> : > Hi,
|> : >
|> : > I was wondering if there is a routine that will read a string and convert
|> : > it to an array of floats. ...
|> :
|> : Have you tried reads?
|> :
|> : a = 0.d0 & b = 0.d0 & c = 0.d0
|> : reads,'9.32323 24323.233 22.124235555d-14',a,b,c
|> : print,c,b,a
|>
How about,
IDL> s = ' 9.32323 24323.233 22.124235555d-14 '
IDL> x = float(splitstr(strcompress(strtrim(s,2)),' '))
Where
;+
;NAME:
; SPLITSTR
;PURPOSE:
; Split a string into an array at specified character boundary
;CATEGORY:
;CALLING SEQUENCE:
; array = splitstr(string,character)
;INPUTS:
; string = string to be split
; character = character to split string on
;OPTIONAL INPUT PARAMETERS:
;KEYWORD PARAMETERS
;OUTPUTS:
; array = string array
;COMMON BLOCKS:
;SIDE EFFECTS:
;RESTRICTIONS:
; character must be a single character string, e.g. ' ' or 'a'
;PROCEDURE:
;EXAMPLES:
; array = splitstr('Hi There',' ')
; array(0) is 'Hi'
; array(1) is 'There'
;MODIFICATION HISTORY:
; T. Metcalf 1994-11-10
;-
function splitstr,line,char
cline = char+string(line(0))+char
place = where(byte(cline) eq (byte(char))(0))
nplace = n_elements(place)
strarray = strarr(nplace-1L)
strarray(0) = strmid(cline,place(0)+1L,place(1)-place(0)-1L)
for i = 1L,nplace-2L do begin
strarray(i) = strmid(cline,place(i)+1L,place(i+1L)-place(i)-1L)
endfor
return,strarray
end
|
|
|