0

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>
David Parks
  • 30,789
  • 47
  • 185
  • 328
  • 1
    Not much that can go wrong here. Is `filesnames` defined and initialized? Is a simple `foreach/for` with maybe just integers working? – Ash Jul 14 '16 at 02:44
  • I tried replacing it with a simple `for`/`endfor`, same problem. I've triple checked filesnames, and reproduced it with a simpler example as posted now. – David Parks Jul 14 '16 at 02:48
  • 1
    Are you sure your question is about `idl` and not `idl-programming-language`? – user4003407 Jul 14 '16 at 02:56

1 Answers1

1

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.

mgalloy
  • 2,356
  • 1
  • 12
  • 10
  • Ahhhhhh :::a sticky slap to the forehead is heard in the distance::: Thanks! – David Parks Jul 14 '16 at 17:55
  • 2
    Or, if you really want to, you can use "&" to make it into a compound statement. For example: foreach element,arr do begin & blah blah & endforeach You could use $ to split it across multiple lines. – Chris Torrence Jul 14 '16 at 22:07