Re: "include" a file [message #66990 is a reply to message #66989] |
Fri, 26 June 2009 12:54  |
JDS
Messages: 94 Registered: March 2009
|
Member |
|
|
On Jun 26, 3:50 pm, JD Smith <jdtsmith.nos...@yahoo.com> wrote:
> Various other languages have the option to include and evaluate the
> program contents of another file at runtime. IDL has the "@"
> operator, but that happens at compile time, so you need to know which
> file to include in advance. I find myself needing to drop small
> "parameter" files in individual directories for a routine to process
> as it crawls through. I could certainly prepare an IDL .sav file, or
> some other data format, parse that, and set-up structures and
> variables as needed, but that makes editing and updating the file very
> painful. What IDL needs is a way to "include" a file directly, and
> evaluate its contents. Finding nothing, I came up with the following
> concept:
>
> ;; include -- Include and evaluate the IDL command contents of a file.
> ;; To use, give the variable "include_file" in the same scope the name
> ;; of a valid file containing IDL commands (batch syntax only), then
> ;; batch include this file, ala:
> ;;
> ;; include_file='/path/to/file'
> ;; @include
> ;;
> _inc_lines=replicate('',file_lines(include_file))
> openr,_inc_un,include_file,/get_lun
> readf,_inc_un,_inc_lines
> _inc_wh=where(~stregex(_inc_lines,'\$(;.*)?[ \t]*$',/
> BOOLEAN),_inc_cnt)
> _inc_start=0L
> for _inc_i=0L,_inc_cnt-1 do begin
> _inc_parts=_inc_lines[_inc_wh[_inc_i]]
> if _inc_wh[_inc_i] gt _inc_start then $
> _inc_parts=strjoin( $
> reform((stregex(_inc_lines[_inc_start:_inc_wh
> [_inc_i]-1],$
> ' *(.*) *\$(;.*)?[ \t]*$', $
> /SUBEXPR,/EXTRACT))[1,*])) +
> _inc_parts
> _inc_void=execute(_inc_parts)
> _inc_start=_inc_wh[_inc_i]+1L
> endfor
> free_lun,temporary( $ ; Clean-up all variables
> (_inc_parts=temporary( $
> (_inc_wh=temporary( $
> (_inc_lines=temporary( $
> (_inc_void=temporary( $
> (_inc_cnt=temporary( $
> (_inc_i=temporary( $
> (_inc_start=temporary(_inc_un)))))))))))))))
>
> This works just fine, collapsing multi-line commands, and executing
> them, at the cost of temporarily polluting the current scope with
> "_inc_" variables (these are left undefined after the @include). You
> have to use "batch syntax", aka as "standalone single line command"
> syntax, but for my purposes this isn't a major limitation. It uses
> execute, so won't work in the IDL_VM, and if you try to do it many
> times in a loop, you might regret it. But for quickly setting up
> human-editable parameter lists, I find it works great.
>
> Do others encounter this problem, and has anyone solved it in a
> different way?
OK, that got poorly formatted by the news relay:
http://tir.astro.utoledo.edu/idl/include.pro
|
|
|