Re: How to check variable type? [message #40504] |
Mon, 16 August 2004 16:49 |
MKatz843
Messages: 98 Registered: March 2002
|
Member |
|
|
Here's a little dual-action function you can use.
;+
; -------------------------
; 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
; equal to the second-to-last field in the "size" function,
; OR if the keyword "string" is set, a type name is returned.
;
; Examples:
; print, var_type(1d) ;--- returns "5"
; print, var_type(1d, /string) ;--- returns "DOUBLE"
; -------------------------
;-
function var_type, a, string = string
return, keyword_set(string) ? size(a, /TNAME) : size(a, /TYPE)
end
biophys@gmail.com (biophysics) wrote in message news:<512b0d8b.0408131520.46e68fbd@posting.google.com>...
> Hi, guys
>
> I want to write a function that processes data very differently
> according to the type. But How do I check the type inline? I couldn't
> find any clue by a quick look through the function list. Can anybody
> give me some hints?
>
|
|
|
Re: How to check variable type? [message #40517 is a reply to message #40504] |
Fri, 13 August 2004 18:26  |
Kenneth P. Bowman
Messages: 585 Registered: May 2000
|
Senior Member |
|
|
In article <512b0d8b.0408131520.46e68fbd@posting.google.com>,
biophys@gmail.com (biophysics) wrote:
> Hi, guys
>
> I want to write a function that processes data very differently
> according to the type. But How do I check the type inline? I couldn't
> find any clue by a quick look through the function list. Can anybody
> give me some hints?
>
> thanks!
You mean it isn't obvious that you use SIZE to get the variable type?
CASE (SIZE(var, /TNAME) OF
'UNDEFINED' : ...
'BYTE' : ...
.
.
.
ELSE : ...
ENDCASE
To make it more obscure, you can use type codes instead of type names.
;-)
Regards, Ken Bowman
|
|
|