Re: variable types [message #11011 is a reply to message #10993] |
Fri, 20 February 1998 00:00   |
Martin Schultz
Messages: 515 Registered: August 1997
|
Senior Member |
|
|
David R. Klassen wrote:
>
> I'm in the process of writing a quick and dirty program to
> go through a text file and grab the lines that are ordered
> pairs of numbers and plot them. The problem comes with the
> fact that some of these data files have a line or two of
> text at the top.
>
> When I read each line of the file I parse it along white-spaces
> or tabs uisng the PARTS function (no problem so far). Then I
> assign the first two parts to my x and y arrays. The problem
> is that if the line read was a line of text, the parts can not
> be converted from string type to float type (it turns out that
> a string ' 13.456' *can* be turned into a float=13.456).
>
> My question: is there a way to test the variable type before
> I make the assignments?
>
yes, you will probably have to read in your line as string (it is really
a pity that the '$' format code does not work during input !), then you
can either go David's way to catch the error or test for a number
yourself:
readf,s [,format='(A)']
test = strpos('0123456789.+-',strmid(strtrim(s,1),0,1)) ge 0
Fortran data files sometimes use a line format like
N x1 x2 x3 ... xN, so you need to extract the first number
before you can read the others. In these cases, you should also read
the line into a string, [test the first character for a number] and
then extract the numbers using READS.
Regards,
Martin.
------------------------------------------------------------ -------
Dr. Martin Schultz
Department for Earth&Planetary Sciences, Harvard University
186 Pierce Hall, 29 Oxford St., Cambridge, MA-02138, USA
phone: (617)-496-8318
fax : (617)-495-4551
e-mail: mgs@io.harvard.edu
IDL-homepage: http://www-as.harvard.edu/people/staff/mgs/idl/
------------------------------------------------------------ -------
|
|
|