[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: help with relational operators

Dave (dmarshall@ivory.trentu.ca) writes:

> I'm getting near wits end.

Yikes! Me, too, but for a completely different reason today. :-)

> repeat begin
> ...
>  ; get user to input file name
> ...
>  dataFName='somefile.xdr'
> ...
>  fileCheck=findfile(dataFName)
>  if fileCheck eq '' then print, 'error: ', dataFName,' file not found'
> endrep until fileCheck ne ''
> % Expression must be a scalar in this context: <BYTE      Array[1]>.
> I just want to verify the file exists before opening it.
> This gives my error message if file does not exist, but craps out if file 
> does exist.
> help, fileCheck ne ''
> tells me it _is_ a scalar expression. ?

Whoops. What you don't know is that FINDFILE *always*
returns an array, even if there is only a single element
in it, as in this case. What you need in the IF statement
is a scalar not an array. You could do this:

   if fileCheck[0] eq '' then print, 'error: ', dataFName,' file not found'

But a better way to handle this is to use the COUNT
keyword to FINDFILE, like this:

   fileCheck=findfile(dataFName, Count=numFiles)
   IF numFiles EQ 0 THEN print, 'error: ', dataFName,' file not found'

Then you don't have to worry about this quirk in IDL. :-)



David Fanning, Ph.D.
Fanning Software Consulting
Phone: 970-221-0438 E-Mail: davidf@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155

[Note: This follow-up was e-mailed to the cited author.]