IDL syntax oddities
posted Tue 9 May 2017 by Michael Galloy under IDLHere are some odd, but totally legal, IDL statements. I would suggest staying away from all of them and use more conventional syntax.
Here’s one that looks like a string, but is really specifying an octal value:
IDL> x = "12
IDL> help, x
X INT = 10
Back when octal values were much more important than now, I suppose it made some sense to have special syntax for entering them. In modern times, I would suggest x = '12'o
.
This also means that if you are specifying a string that begins with digits 0-7 with double quotes, you can generate a bewildering syntax error:
IDL> y = "12 monkeys"
y = "12 monkeys"
^
% Syntax error.
I recommend using single quotes for all strings, i.e., y = '12 monkeys'
.
Next up is a convenience for the truly lazy:
IDL> s = 'some string
You don’t have to put the trailing single or double quote on a string if it is the last character on the line. This will probably make your text editor’s syntax highlighting confused. One character is not too much to type for some clarity.
Finally, I just saw this one last week:
IDL> for i = 0, 4 do begin y = i & print, i
0
1
2
3
4
There are quite a few problems with this:
- There is a
begin
with no matchingend
! - There is not a
&
after thebegin
even though you would normally have to start a new line there. - I would recommend against using
&
. It can be useful on the command line (it makes it easier to up arrow to a previous set of commands), but don’t do it in a file!
This is the standard syntax for that line (if you really need to put it all on a single line):
IDL> for i = 0, 4 do begin & y = i & print, i & endfor
I might count using parentheses for indexing arrays as a syntax oddity as well, but there are so many IDL programmers still doing it that it counts as commonplace. I still recommend against it.
May 9th, 2017 at 7:00 pm
I’ve been using IDL for years and just the other day I found out the following is legal
x=(y=(z=5))
May 10th, 2017 at 8:30 am
The parenthesis-for-array-indexing is one of the worst design decisions I’ve every come across. It prevents the interpreter from deciding whether a function call or an array index is being used. It results in syntax errors when you try to call a function that IDL can’t find/compile, IF the function is called with a keyword argument:
I got this error trying to run some legacy code (b/c I hadn’t configured my IDL path to find missing_function, which was inside some_big_library.pro). Took me ages to figure out what the issue was!
May 10th, 2017 at 3:50 pm
@Lucas I agree. I am totally strictarr.