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

Home » Public Forums » archive » Test if variable is a string
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
Test if variable is a string [message #33913] Tue, 04 February 2003 01:56 Go to next message
Carsten Dominik is currently offline  Carsten Dominik
Messages: 45
Registered: February 1998
Member
Hello,

I must be blind: How do I test for the type of the object contained
in a variable? I am writing a procedure and want to allow one of the
arguments to be either a string or a number - but the program must of
course find out which it is?

This is my main problem with IDL: I am *never* able to correctly guess
the name of a function for a particular purpose.

So what is the magic word for determining the type of a variable?
Thanks.

- Carsten
Re: Test if variable is a string [message #34025 is a reply to message #33913] Sat, 15 February 2003 05:28 Go to previous messageGo to next message
R.Bauer is currently offline  R.Bauer
Messages: 1424
Registered: November 1998
Senior Member
Carsten Dominik wrote:
> Hello,
>
> I must be blind: How do I test for the type of the object contained
> in a variable? I am writing a procedure and want to allow one of the
> arguments to be either a string or a number - but the program must of
> course find out which it is?
>
> This is my main problem with IDL: I am *never* able to correctly guess
> the name of a function for a particular purpose.
>
> So what is the magic word for determining the type of a variable?
> Thanks.
>
> - Carsten
>
>

Dear Carsten,


a bit late but I believe the opposite check is better to test if the
string is a regular number because if you test only about types it could
happen that's an exponent number is defined as string.


I often use a routine named string_is_number

you'll find it here:

http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_source/idl _html/dbase/download/string_is_number.tar.gz



regards

reimar




--
Reimar Bauer

Institut fuer Stratosphaerische Chemie (ICG-I)
Forschungszentrum Juelich
email: R.Bauer@fz-juelich.de
------------------------------------------------------------ -------
a IDL library at ForschungsZentrum Juelich
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_lib_intro. html
============================================================ =======
Re: Test if variable is a string [message #34108 is a reply to message #33913] Mon, 17 February 2003 02:36 Go to previous message
R.Bauer is currently offline  R.Bauer
Messages: 1424
Registered: November 1998
Senior Member
M. Katz wrote:


Did you recognize the tname keyword of size?

IDL> print,size(10L,/tname)
LONG

regards
Reimar

> Here's a handy little function I've been using for years.
> var_type.pro
> I think I added a number of new type names back in 2001.
> If RSI adds more types in the future, they'll probably
> be appended to the list rather than change the existing list.
> In that case, you'll have to modify the list.
>
> So the function returns the type as a number. But if
> you set the /string keyword, you'll get a string containing
> the name of the type.
>
> For example:
> IDL> print, var_type(10L)
> 3
> IDL> print, var_type(10L, /s)
> Longword Integer
>
> ;+
> ; IDL function: var_type.pro
> ; for a given input variable, this function returns
> ; the type of that variable. The returned value is a number 0-15
> ; similar to the second-to-last field in the "size" function,
> ; or if the keyword "string" is used, a string.
> ;-
>
> function var_type, a, string=string
>
> s = ['Undefined','Byte','Integer','Longword
> Integer','Floating-point', $
> 'Double-precision floating','Complex
> floating','String','Structure', $
> 'Double-precision complex','Pointer','Object reference', $
> 'Unsigned Integer','Unsigned Longword Integer','64-bit
> Integer',$
> 'Unsigned 64-bit Integer']
>
> return, Keyword_set(string) ? s(size(a, /type)):size(a, /type)
> end
>
> I also have a closely related function called defined.pro that tests
> true-or-false, if a given variable is defined or not. It also uses
> size(x, /type), that's why I'm including it here.
>
> ;+
> ; IDL function: defined.pro
> ; For a given input variable, this function returns
> ; 1b if the variable is defined
> ; 0b if the variable is undefined
> ;-
>
> function defined, a
> return, (size(a, /type) NE 0)
> end
>
>
> Hope this helps,
>
> M. Katz


--
Reimar Bauer

Institut fuer Stratosphaerische Chemie (ICG-I)
Forschungszentrum Juelich
email: R.Bauer@fz-juelich.de
------------------------------------------------------------ -------
a IDL library at ForschungsZentrum Juelich
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_lib_intro. html
============================================================ =======
Re: Test if variable is a string [message #34114 is a reply to message #34025] Sun, 16 February 2003 12:58 Go to previous message
MKatz843 is currently offline  MKatz843
Messages: 98
Registered: March 2002
Member
Here's a handy little function I've been using for years.
var_type.pro
I think I added a number of new type names back in 2001.
If RSI adds more types in the future, they'll probably
be appended to the list rather than change the existing list.
In that case, you'll have to modify the list.

So the function returns the type as a number. But if
you set the /string keyword, you'll get a string containing
the name of the type.

For example:
IDL> print, var_type(10L)
3
IDL> print, var_type(10L, /s)
Longword Integer

;+
; IDL function: var_type.pro
; for a given input variable, this function returns
; the type of that variable. The returned value is a number 0-15
; similar to the second-to-last field in the "size" function,
; or if the keyword "string" is used, a string.
;-

function var_type, a, string=string

s = ['Undefined','Byte','Integer','Longword
Integer','Floating-point', $
'Double-precision floating','Complex
floating','String','Structure', $
'Double-precision complex','Pointer','Object reference', $
'Unsigned Integer','Unsigned Longword Integer','64-bit
Integer',$
'Unsigned 64-bit Integer']

return, Keyword_set(string) ? s(size(a, /type)):size(a, /type)
end

I also have a closely related function called defined.pro that tests
true-or-false, if a given variable is defined or not. It also uses
size(x, /type), that's why I'm including it here.

;+
; IDL function: defined.pro
; For a given input variable, this function returns
; 1b if the variable is defined
; 0b if the variable is undefined
;-

function defined, a
return, (size(a, /type) NE 0)
end


Hope this helps,

M. Katz
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Printing line number of an error
Next Topic: UNIQ without SORT

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

Current Time: Wed Oct 08 19:05:21 PDT 2025

Total time taken to generate the page: 0.00648 seconds