comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » function to convert string to array???
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
function to convert string to array??? [message #3843] Wed, 15 March 1995 11:45 Go to previous message
davis is currently offline  davis
Messages: 15
Registered: March 1995
Junior Member
Hi,

I was wondering if there is a routine that will read a string and convert
it to an array of floats. For example,

str = " 10.8 8.4 9.3";
x = string_to_array (str);

would yield an array of 3 elements: x = [10.8 8.4 9.3]

whereas string_to_array (" 20.1 9.2") yields the two element array
[20.1 9.2]. Here is my implementation of this:

FUNCTION string_to_array, str
str = strtrim (strcompress (str), 2);
maxpos = strlen (str)

this_pos = 0
n = 1

while (1) do begin
this_pos = strpos (str, " ", this_pos)
if (this_pos eq -1) then goto, break1
n = n + 1
this_pos = this_pos + 1
endwhile
break1:

a = fltarr (n);

; Now go back and fill the array

maxlen = strlen (str)

for i = 0, n - 2 do begin
reads, str, ai
a(i) = ai
str = strmid (str, 1 + strpos (str, " ", 0), maxlen)
endfor
;
; Now the last element
;
reads, str, ai
a(n - 1) = ai

return, a

END

Thanks,
--John
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Re: [Q}:Rubberbanding in IDL
Next Topic: Default Color for IDL Widgets.

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Thu Oct 09 11:22:19 PDT 2025

Total time taken to generate the page: 1.12152 seconds