[Thread Prev][Thread Next][Index]

Re: [ferret_users] How can I check existence of a variable



Hi Patrick,
That's a good idea, to have a RETURN hand us back the word ERROR; it would come in handy when writing scripts.

For your case, you can get the information you want right now, using the attribute-handling capabilities of Ferret. For a full description of all this, see section 3.1.8 in the Users guide - http://ferret.pmel.noaa.gov/Ferret/documentation/users-guide/variables-xpressions/VARIABLES#_VPINDEXENTRY_277

Scroll on down in this section to section 3.1.8.2, Attribute Keywords

We will be using the attribute keyword, varnames, to define a variable containing the names of the variables in the current dataset

  yes? use levitus_climatology.cdf
yes? let allnames = ..varnames
  yes? let temp_exists = IS_ELEMENT_OF_STR (allnames, "TEMP")

Note that this is case-sensitive -- we could use a version of the IS_ELEMENT_OF_STR function which makes a case-insensitive comparison, but you could of course do

  yes? let temp_exists = `IS_ELEMENT_OF_STR (allnames, "TEMP") OR \
       IS_ELEMENT_OF_STR (allnames, "TEMP")`


Ansley


Brockmann Patrick wrote:
Hi all,

In 2005, someone asks how to check if a variable exists.
Read http://www.pmel.noaa.gov/maillists/tmap/ferret_users/fu_2005/msg00828.html

I have tried to use return value but the error message is not trappable

yes? use levitus_climatology.cdf
yes? def sym isexist=`temp,return=dsetnum`
!-> def sym isexist=1
yes? def sym isexist=`abcd,return=dsetnum`
**ERROR: variable unknown or not in data set: ABCD

So is there a way to improve return value mechanism to trap the error message and get a return value set to "ERROR". It would be easy then to test it with
a simple test:

check.jnl

def sym isexist=`($01),return=dsetnum`
if `"($isexist)" eq "ERROR"` then
       say "($01) does not exist"
elif
       say "($01) exists"
fi

Thanks
Patrick


[Thread Prev][Thread Next][Index]

Contact Us
Dept of Commerce / NOAA / OAR / PMEL / TMAP

Privacy Policy | Disclaimer | Accessibility Statement