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

Home » Public Forums » archive » How to see if variable is defined?
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
How to see if variable is defined? [message #3359] Fri, 20 January 1995 09:43 Go to next message
rfinch is currently offline  rfinch
Messages: 51
Registered: March 1991
Member
Is there a nice way of checking if a variable is defined?

I have a widget_control command which removes a menu, and I need to
see if the menu is defined before destroying it.
--
Ralph Finch 916-653-8268:voice 916-653-6077:fax
rfinch@dop.water.ca.gov / finger for PGP public key
"Nada burra la chamaca." A.G.
Any opinions expressed are my own; they do not represent my employer
Re: How to see if variable is defined? [message #3404 is a reply to message #3359] Thu, 26 January 1995 12:28 Go to previous messageGo to next message
zawodny is currently offline  zawodny
Messages: 121
Registered: August 1992
Senior Member
In article <3g8n7i$ghv@sun2.ruf.uni-freiburg.de> ps@kis.uni-freiburg.de (Peter Suetterlin) writes:
> Mike Schienle (rep2857@sbsun0010.sbrc.hac.com) wrote:
> : IDL> print, var
> : % PRINT: Variable is undefined: VAR.
> : % Execution halted at $MAIN$ (PRINT).
> : IDL> print, size(var)
> : 0 0 1
>
> : The first parameter will be a zero if var is undefined.
>
> !!NO
>
> that's wrong:
> IDL> i=1
> IDL> print,size(i)
> 0 2 1
> IDL>
>
> The *second* parameter has to be zero (exactly s(s(0)+1, which defines
> the type of the variable)


I've noticed that this thread has survived longer than I would
anticipate from the subject. I have not been following it so, this may have
been pointed out already.

I use the following:

if(n_elements(var) eq 0) then print,'Variable is undefined'

This is much easier to remember and is less code than using SIZE.

--
Joseph M. Zawodny (KO4LW) NASA Langley Research Center
Internet: j.m.zawodny@larc.nasa.gov MS-475, Hampton VA, 23681-0001
TCP/IP: ko4lw@ko4lw.ampr.org Packet: ko4lw@n4hog.va.usa.na
Re: How to see if variable is defined? [message #3411 is a reply to message #3359] Thu, 26 January 1995 03:18 Go to previous messageGo to next message
nmw is currently offline  nmw
Messages: 18
Registered: January 1995
Junior Member
In article 95Jan20094331@venice.water.ca.gov, rfinch@water.ca.gov (Ralph Finch) writes:
> Is there a nice way of checking if a variable is defined?
>
> I have a widget_control command which removes a menu, and I need to
> see if the menu is defined before destroying it.
> --
> Ralph Finch 916-653-8268:voice 916-653-6077:fax
> rfinch@dop.water.ca.gov / finger for PGP public key
> "Nada burra la chamaca." A.G.
> Any opinions expressed are my own; they do not represent my employer


There are two possible ways of doing this.

The first is to use the keyword_set function. This returns 1 if a variable
is defined, 0 otherwise. Use of this function to test for a variable being
defined is not recommended and is bad practice - it is meant to test for
the existence of a keyword in a function call, it happens to work.

The proper way is to use the size function. This returns a vector describing
the attributes of a variable. Unfortunately it is a variable length vector
which can make programming using it quite tedious. However, just to check
if a variable is defined is quite simple. The second element of the vector
is the data type of the variable, which is zero if the variable is undefined.

Thus, the code

IF (SIZE(variable))(1) EQ 0 THEN BEGIN
PRINT,'Undefined'
ENDIF ELSE BEGIN
PRINT.'defined'
ENDELSE

should tell you what you want to know.


---
------------------------------------------------------------ ------
Nigel Wade | E-mail : nmw@ion.le.ac.uk
System Administrator |
Ionospheric Physics Group | phone : +44 (0)116 2523568
University of Leicester |
Leicester, LE1 7RH, UK | fax. : +44 (0)116 2523555
------------------------------------------------------------ -------
Re: How to see if variable is defined? [message #3435 is a reply to message #3359] Tue, 24 January 1995 08:22 Go to previous messageGo to next message
rep2857 is currently offline  rep2857
Messages: 28
Registered: December 1994
Junior Member
In article <RFINCH.95Jan20094331@venice.water.ca.gov>,
Ralph Finch <rfinch@water.ca.gov> wrote:
> Is there a nice way of checking if a variable is defined?
>
> I have a widget_control command which removes a menu, and I need to
> see if the menu is defined before destroying it.

In the general case of checking whether a variable is defined you can
use the size() function. For example:

IDL> print, var
% PRINT: Variable is undefined: VAR.
% Execution halted at $MAIN$ (PRINT).
IDL> print, size(var)
0 0 1

The first parameter will be a zero if var is undefined.

For widgets in particular, the widget_info function has a "valid_id"
keyword which can check to see if the widget is a valid ID. Based on
the return from the call you can destroy the widget with
widget_control, /destroy.

if (Widget_Info(tac_base, /valid_id)) then $
Widget_Control, tac_base, /Destroy

Mike Schienle Hughes Santa Barbara Research Center
rep2857@sbsun0010.sbrc.hac.com 75 Coromar Drive, M/S B28/87
Voice: (805)562-7466 Fax: (805)562-7881 Goleta, CA 93117
Re: How to see if variable is defined? [message #3450 is a reply to message #3359] Mon, 23 January 1995 07:47 Go to previous messageGo to next message
thompson is currently offline  thompson
Messages: 584
Registered: August 1991
Senior Member
rfinch@water.ca.gov (Ralph Finch) writes:

> Is there a nice way of checking if a variable is defined?

> I have a widget_control command which removes a menu, and I need to
> see if the menu is defined before destroying it.

You can always use

IF N_ELEMENTS(variable) NE 0 THEN ...

Bill Thompson
Re: How to see if variable is defined? [message #3497 is a reply to message #3411] Fri, 27 January 1995 06:49 Go to previous message
thompson is currently offline  thompson
Messages: 584
Registered: August 1991
Senior Member
nmw@ion.le.ac.uk (Nigel Wade) writes:

> In article 95Jan20094331@venice.water.ca.gov, rfinch@water.ca.gov (Ralph Finch) writes:
>> Is there a nice way of checking if a variable is defined?
>>
>> I have a widget_control command which removes a menu, and I need to
>> see if the menu is defined before destroying it.
>> --
>> Ralph Finch 916-653-8268:voice 916-653-6077:fax
>> rfinch@dop.water.ca.gov / finger for PGP public key
>> "Nada burra la chamaca." A.G.
>> Any opinions expressed are my own; they do not represent my employer


> There are two possible ways of doing this.

> The first is to use the keyword_set function. This returns 1 if a variable
> is defined, 0 otherwise. ...

Not quite true. KEYWORD_SET returns 1 if a variable is defined AND IS NOT
ZERO!

Bill Thompson
Re: How to see if variable is defined? [message #3499 is a reply to message #3435] Thu, 26 January 1995 09:47 Go to previous message
ps is currently offline  ps
Messages: 14
Registered: September 1994
Junior Member
Mike Schienle (rep2857@sbsun0010.sbrc.hac.com) wrote:
: IDL> print, var
: % PRINT: Variable is undefined: VAR.
: % Execution halted at $MAIN$ (PRINT).
: IDL> print, size(var)
: 0 0 1

: The first parameter will be a zero if var is undefined.

!!NO

that's wrong:
IDL> i=1
IDL> print,size(i)
0 2 1
IDL>

The *second* parameter has to be zero (exactly s(s(0)+1, which defines
the type of the variable)

Peter

------------------ Peter 'PIT' Suetterlin -----------------
| Kiepenheuer Institut | Sternfreunde Breisgau e.V |
| fuer Sonnenphysik | |
| 0761/3198-210 | 0761/71571 |
-<ps@kis.uni-freiburg.de>-<suettpet@sun1.ruf.uni-freiburg.de>--
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: autostart Wave license, Solaris 2.4
Next Topic: "True" map projections

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

Current Time: Wed Oct 08 11:35:19 PDT 2025

Total time taken to generate the page: 0.00724 seconds