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
|
|
|
Re: function to convert string to array??? [message #3821 is a reply to message #3816] |
Fri, 17 March 1995 08:24  |
dnguyen
Messages: 4 Registered: May 1994
|
Junior Member |
|
|
In article <3k89l3$2ap@news.csus.edu>, Chris McCarthy <chris@quark.sfsu.edu> says:
>
> 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
>
>
> -chris
>
Here is another way to do it :
wave > a = ' 100. , 200. , 300. , 400. '
wave > a = '[' + a + ']'
wave > z = execute('myarray = '+a)
wave > info,myarray
myarray FLOAT = ARRAY(4)
Derrick
|
|
|
Re: function to convert string to array??? [message #3831 is a reply to message #3821] |
Thu, 16 March 1995 20:58  |
davis
Messages: 15 Registered: March 1995
|
Junior Member |
|
|
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
The code that I posted uses `reads'. The problem is that you have to
determine how many floats the string is composed of and then allocate an
array of that size.
--John
|
|
|
|