This is an ever-expanding list of IDL Tips and Tricks to make your coding easier and quicker.

Finding Files Using Open Resource


If you know at least part of the name of the file you're looking for, Open Resource can be an extremely fast way to find and open it. In IDL Workbench's IDL Console tab, press <Ctrl><Shift><R> to bring up Open Resource. Start typing in the text box at the top of the window (wildcards are OK), and the window will incrementally display and filter down a list of every matching file in your open projects. Press <ENTER> to open the top item in the list. Open Resource also works well for opening multiple files with similar names.

Note: Open Resource only searches the files found in your IDL Workbench's Project Explorer.

Search in Workbench Editor using Incremental Find


Use Incremental Find to quickly search for text or numbers in the file currently open in your Workbench editor. Click inside the editor window, press <CTRL-J>, and type your search string. IDL searches inside your open file for matches and incrementally refines the results as you type. Use the up and down arrow keys on your keyboard to jump to the previous or next matches in the file. To repeat the last search, press <CTRL-J> again then use the up or down arrow keys to go to the nearest match.

Obtain Information on a Routine


Sometimes there are collisions between *.PRO code in your workspace and *.SAV files that are loaded into the runtime, leading to inconsistent builds when routines you thought were from the *.SAV file are from *.PRO code, or vice versa. The ROUTINE_FILEPATH() function will tell you which version of a routine is being used.

Example

In the IDL console, enter:

nv = ENVI(/DEBUG)

At the resulting ENVI prompt, enter:

PRINT, ROUTINE_FILEPATH('envi__define')

The console displays something similar to (depending on your ENVI/IDL installation):

{ ENVI__DEFINE C:\Program Files\***\ENVIxx\save\envi
_framework.sav}

Objects


Class Hierarchy Browser

The Class Hierarchy browser is an IDL Workbench view that displays the structure of the object hierarchy. To expose this feature, select Window > Class Hierarchy.

Find the Members of an Object

To know the member variables of a given object or class type, you can use the Class Hierarchy dialog (above), or you can find it programmatically by creating a structure that is of the same type.

Example:

At the IDL prompt in Workbench, type:

HELP, {IDLgrPLOT}
   

Programming Shortcuts


Ternary Operator "?:" Instead of IF-THEN-ELSE

You can use the ternary operator "?:" as shorthand for IF-THEN-ELSE statements. The general syntax is:

(expression with condition) ? expression1 : expression2

If the condition is true, then IDL substitutes  expression1.

If the condition is false, then IDL substitutes expression2. Below are some examples:

Example 1

Use:

z = (a GT b) ? a : b

Instead of:

IF (a gt b) THEN z = a ELSE z = b

The ternary operator assigns the value of a to z if a is greater than b. If a is not greater than b, then b will be assigned to z.

Example 2

Use this:

x = RANDOMU(seed)
y = (x GT .5) ? (x - .5) : (x + .5)
PRINT, y

Or this:

x = RANDOMU(seed)
IF (x GT .5) THEN y = x - .5 ELSE y = x + .5
PRINT, y

Instead of:

x = RANDOMU(seed)
IF x GT .5 THEN BEGIN
   y = x - .5
ENDIF ELSE BEGIN
   y = x + .5
ENDELSE
PRINT, y
 

CASE and SWITCH

Use CASE or SWITCH to simplify multiple IF-THEN-ELSE structures.

CASE tests each case until it finds one that is true, then executes that statement and ignores all subsequent statements. SWITCH is similar to CASE except that SWITCH executes the first matching statement PLUS any following statements in the SWITCH block.

See CASE Versus SWITCH for more information.