Search code examples
idlidl-programming-language

Syntax error on FOREACH ... ENDFOREACH loop


I've got what seems like a simple FOREACH loop in IDL (version 8.2.2). For the life of me I can't see why I'm getting a syntax error. I tried replacing the FOREACH with a simple FOR loop with the same results.

Removing the for loop and running the statements alone works fine (hard coding a single value for file of course).

FOREACH file, filenames DO BEGIN
   ; A number of
   ; statements that execute
   ; just fine
ENDFOREACH

Result:

ENDFOREACH
 ^
% Syntax error.

This is all running in a script, called with @myscript

Perhaps an even simpler example straight from the documentation will help:

I created a script test.pro, copy/paste from the docs: http://www.harrisgeospatial.com/docs/BEGIN___END.html, the contents of test.pro are:

arr = [1, 3, 5, 7, 9]
FOREACH element, arr DO BEGIN
   PRINT, element
ENDFOREACH

Result:

IDL> @test
       9

ENDFOREACH
 ^
% Syntax error.
  At: /mydir/test.pro, Line 4
IDL>

Solution

  • Batch files (called via @myscript) can't have compound statements, i.e. with BEGIN/END. Make it into a procedure/function or a main-level program.