next up previous contents
Next: Default array operations Up: Lecture 2: Programming in Previous: Other flow control statements   Contents

Array operations

IDL allows you to do many operations on whole arrays. Suppose, for example, you wanted to make an array whose elements were the squares of the elements in another array. You could use a for loop, like this:

 
xsquared = fltarr(n)
for j=0,n=1 do xsquared(j) = x(j) * x(j)

This is how you would have to go about it in C or Fortran. In IDL, however, you can do this:

 
xsquared = x*x

This will clearly make your code shorter and clearer but it will also make it much faster. IDL is not a true compiled language like C. When it tells you it has

 
\% Compiled module: $MAIN$.
it has checked the syntax and converted it to an internal form which is more efficient than interpreting what you typed in directly. (Perl wizards will know what I mean.) It does NOT compile the program into machine code like the C compiler does. The upshot of this is that an IDL program with too many `for' and `while' loops, `if' statements etc. can be very slow. If you can replace the loops and ifs with array operations, the program will be a LOT faster - as fast as a C program in the best cases.



Subsections
next up previous contents
Next: Default array operations Up: Lecture 2: Programming in Previous: Other flow control statements   Contents
John Marsham 2005-04-22