next up previous contents
Next: arrays Up: Lecture 2: Programming in Previous: Lecture 2: Programming in   Contents

Variables, data types

As with most languages, IDL has several types of variables. I list some of them here:

type range bytes to define to convert
byte 0 to 255 1 b=15B b=byte(x)
integer -32768 to +32767 2 i=15 i=fix(x)
long -2147483648 to +2147483647 4 j=long(15) j=long(x)
      j=147483647  
floating pt $\pm 10^{38}$, 7 sig figs 4 y=1.7 y=float(x)
double prec. $\pm 10^{308}$, 14 sig figs 8 y=1.7d0 d=double(x)
complex two floating point no.s 8 z=complex(1.2,0.3) z=complex(x)
string (used for text) 0-32767 s='blah' s=string(x)

Variable names can have letters, numbers and underscores in them. They are NOT case-sensitive: foo, Foo and FOO are all the same variable.

Note that the default type of integer is only a 2-byte integer. If you want a long integer you have to remember to ask for it. Remember if you want a big loop index variable! BUT remember loops are BAD in IDL.

You can convert one variable from one form to another using fix. Eg

str_var=fix(153.15,type=7) ;sets str_var to a string
help,str_var 
;STR_VAR         STRING    = '      153.150'; shows that it is a string, but it has much white space
;remove this with
str_var=strtrim(str_var,2); 2 removes white space from the front and back
Again I do this so often that I have a routine called trmfix that combines fix,type=7 and strtrim.

IDL is a dynamically typed language. That means that you don't have to define your variables or say which variable is which type at the start of your program. You can say gak=37.5d0 at any point in your program and a double precision variable called gak will spring into existence and take on the value 37.5 . If you had a variable of a different type called gak at some earlier point in your program, it will vanish when you create the double precision variable with the same name.



Subsections
next up previous contents
Next: arrays Up: Lecture 2: Programming in Previous: Lecture 2: Programming in   Contents
John Marsham 2005-04-22