3666
How to fix the compilation error “Only 8 subscripts allowed” in IDL
Reproduce Case
- Save the below codes in a single .pro file called test_arg.pro.
PRO test_arg
print,func_test_arg(1,2,3,4,5,6,7,8,9)
END
FUNCTION func_test_arg,a,b,c,d,e,f,g,h,i
f=a+b+c+d+e+f+g+h+i
RETURN, f
END
- Compile it: it returns the below error
IDL> .compile -v 'C:\_BERANGERE\TEST_PG\test_arg.pro'
print,func_test_arg(1,2,3,4,5,6,7,8,9)
^
% Only 8 subscripts allowed.
At: C:\_BERANGERE\TEST_PG\test_arg.pro, Line 2
% 1 Compilation error(s) in module TEST_ARG.
% Compiled module: FUNC_TEST_ARG.
Why this error?
This error is due to a misinterpretation by the compiler of line 2 of the test code
func_test_arg(1,2,3,4,5,6,7,8,9)
func_test_arg is interpreted as an array instead of a function name.
The reason of this misinterpretation is the function func_test_arg which is located after the main procedure that is calling it in the .pro file. IDL compiler is working line by line. Thus, the function func_test_arg itself – defined at line 5 of the .pro file - is not yet compiled when the compiler is compiling line 2:
func_test_arg(1,2,3,4,5,6,7,8,9)
is then interpreted as follows
func_test_arg is an array
(1,2,3,4,5,6,7,8,9) are the subscripts to locate a specific value in this 9-dimension array
Knowing that IDL does not support array of more than 8 dimensions, an error is caught by the compiler mentioning the limitation to 8 subscripts, i.e. 8 dimensions.
A similar error can even be reproduced at the prompt when working with arrays:
IDL > a=intarr(2,2,2)
IDL > print, array(2,1,1,3,2,3,2,5,5)
It returns
print, array(2,1,3,3,2,3,2,5,5)
^
% Only 8 subscripts allowed.
Workarounds/suggestions
Different approaches can be considered to prevent such compilation error
- Option 1:
- save each function/procedure in a separate .pro file called with the function/procedure name. For example: remove the below lines of code from test_arg.pro and save them in a separate .pro files called func_test_arg.pro.
FUNCTION func_test_arg,a,b,c,d,e,f,g,h,i
f=a+b+c+d+e+f+g+h+i
RETURN, f
END
- add the path of test_arg.pro and func_test_arg.pro to the IDL_PATH: from the IDL workbench select Window > Preferences then IDL > Paths and add the appropriate path in the right part of the dialog window. Select it using the checkbox and click Apply and Close.
- Then restart IDL and try again to compile your .pro
When done the func_test_arg function will be included on IDL’s search path and will be successfully identified as a function when compiling the line 2 of the test_arg.pro code:
func_test_arg(1,2,3,4,5,6,7,8,9)
It should then no longer return a compilation error:
IDL> .compile -v 'C:\_BERANGERE\TEST_PG\test_arg.pro'
% Compiled module: TEST_ARG.
% Compiled module: FUNC_TEST_ARG.
- Option 2: always put the procedures or functions called by other procedures/functions at the top of the .pro files so they will be compiled prior to being called
Example:
FUNCTION func_test_arg,a,b,c,d,e,f,g,h,i
f=a+b+c+d+e+f+g+h+i
RETURN, f
END
PRO test_arg
print,func_test_arg(1,2,3,4,5,6,7,8,9)
END
Compilation will then succeed
IDL> .compile -v 'C:\_BERANGERE\TEST_PG\test_arg.pro'
% Compiled module: FUNC_TEST_ARG.
% Compiled module: TEST_ARG.
- Option 3: use the COMPILE_OPT IDL2 option:
Example:
PRO test_arg
COMPILE_OPT IDL2
print,func_test_arg(1,2,3,4,5,6,7,8,9)
END
FUNCTION func_test_arg,a,b,c,d,e,f,g,h,i
COMPILE_OPT IDL2
f=a+b+c+d+e+f+g+h+i
RETURN, f
END
This compilation option is a combination of the DEFINT32 and STRICTARR options – see link below
https://www.l3harrisgeospatial.com/docs/COMPILE_OPT.html
While compiling, IDL will then only allow square brackets to be used to index arrays. It will not allow parentheses. If IDL encounters parentheses, it will assume that this is being used for a function call.
Note that this last option may require updates to your existing code if you are using parenthesis for subscripting arrays, as IDL may now return errors during execution.
Compilation will then succeed:
IDL> .compile -v 'C:\_BERANGERE\TEST_PG\test_arg.pro'
% Compiled module: FUNC_TEST_ARG.
% Compiled module: TEST_ARG.
Conclusion/best practices
As a conclusion, Option 1 should be enough to fix this specific error. However, Option 2 and 3 are also best practices when working with IDL to prevent compilation errors.
----------------------------------------------------------
created by BC on 8/30/2021 - reviewed by BC(US)