Re: variable types [message #11018 is a reply to message #11011] |
Thu, 19 February 1998 00:00  |
davidf
Messages: 2866 Registered: September 1996
|
Senior Member |
|
|
David R. Klassen (klassen@marswatch.tn.cornell.edu) writes:
> 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?
If I understand the question correctly the variable type
before the assignments is always STRING, whether the
assignment succeeds or not.
What I would try to do is CATCH the assignment error, thinking
that if the assignment to a FLOAT succeeds, the string must
have been a "number". If it doesn't, I'll just read the next
line. My code might look like this:
line = ''
FOR j=0,n-1 DO BEGIN
Catch, error
IF error NE 0 THEN line = ''
ReadF, lun, line
partA = Part(line)
thisNum = Float(partA) ; This is where error occurs.
ENDFOR
The assignment error causes IDL to set the error variable
to the error number and execution jumps to the next line
of code *after* the Catch error handler. In this case, you
just reinitialize the line variable to a string and away you
go.
Later on you might want to cancel the Catch error handler:
Catch, /Cancel
or set another one, etc. Remember that ON_IOERROR will take
precedent over the Catch, so be sure you have it turned off.
Cheers,
David
-----------------------------------------------------------
David Fanning, Ph.D.
Fanning Software Consulting
E-Mail: davidf@dfanning.com
Phone: 970-221-0438
Coyote's Guide to IDL Programming: http://www.dfanning.com/
|
|
|