comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Stupid Question#1
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: Stupid Question#1 [message #13289] Tue, 03 November 1998 00:00
Harald Frey is currently offline  Harald Frey
Messages: 41
Registered: March 1997
Member
rob cosgrave wrote:

> Hi Folks,
> am having a spot of bother with smething that should be really
> simple. I cannot get FOR ....DO BEGIN ENDFOR loops to work. I thought
> first it was something to do with my program, so I tried a really simple
> one, and that didn't work. Then I cut and paste the example out of the
> online documentation and that didn't work. And then I tried running one
> of the scripts which came with IDL and that didn't work either. They
> all failed at the endfor, and didn't close the loop.
> I thought it was just me doing something stupid, but when
> the programs that came with it failed also...
> My version is the IDL student on a Win95 machine. Could the
> student editionness be the problem?
> Again, apologies for the dumb question, but this is driving me crazy,
> and it's kinda hard to program without loops!
> thanks
> Rob Cosgrave

It's hard to make a diagnostics without the lines that failed. However, I assume
that you tried the FOR-loop in the interactive mode and did not take into
account that this works only on one line and that you have to combine multiple
statements with the "$" and/or "&" characters.

One simple example:

for i=0,10 do begin & print,i & j=i^2 & print,j & endfor

Harald frey
hfrey@ssl.berkeley.edu
Re: Stupid Question#1 [message #13308 is a reply to message #13289] Mon, 02 November 1998 00:00 Go to previous message
davidf is currently offline  davidf
Messages: 2866
Registered: September 1996
Senior Member
Hi Folks,

> am having a spot of bother with smething that should be really
> simple. I cannot get FOR ....DO BEGIN ENDFOR loops to work.

This question reminds me that I have several books that
are "factory seconds"--books that are slightly blemished
in some way. I always feel guilty charging full price for
these books, so I have been selling them to students at
greatly reduced prices. If anyone is interested, contact
me. There are almost always one or two hanging around.

Cheers,

David

----------------------------------------------------------
David Fanning, Ph.D.
Fanning Software Consulting
E-Mail: davidf@dfanning.com
Phone: 970-221-0438, Toll-Free Book Orders: 1-888-461-0155
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Re: Stupid Question#1 [message #13309 is a reply to message #13308] Mon, 02 November 1998 00:00 Go to previous message
Kevin Ivory is currently offline  Kevin Ivory
Messages: 71
Registered: January 1997
Member
rob cosgrave wrote:
> am having a spot of bother with smething that should be really
> simple. I cannot get FOR ....DO BEGIN ENDFOR loops to work. I thought
> first it was something to do with my program, so I tried a really simple
> one, and that didn't work. Then I cut and paste the example out of the
> online documentation and that didn't work. And then I tried running one
> of the scripts which came with IDL and that didn't work either. They
> all failed at the endfor, and didn't close the loop.

Block statements (statements with BEGIN and END) only work in procedures
and functions, not in interactive mode.

If you need to learn how to program procedures/functions, you probably should
start off with the online books 'Using IDL' and 'Building IDL Applications'.
You will find them by pressing the 'Contents' button of the IDL online help.

Best regards,
Kevin
--
Kevin Ivory Tel: +49 5556 979 434
Max-Planck-Institut fuer Aeronomie Fax: +49 5556 979 240
Max-Planck-Str. 2 mailto:Kevin.Ivory@linmpi.mpg.de
D-37191 Katlenburg-Lindau, GERMANY http://www.gwdg.de/~kivory2/
Re: Stupid Question#1 [message #13310 is a reply to message #13308] Mon, 02 November 1998 00:00 Go to previous message
davidf is currently offline  davidf
Messages: 2866
Registered: September 1996
Senior Member
rob cosgrave (r.cosgrave@ucc.ie) writes:

> am having a spot of bother with smething that should be really
> simple. I cannot get FOR ....DO BEGIN ENDFOR loops to work. I thought
> first it was something to do with my program, so I tried a really simple
> one, and that didn't work. Then I cut and paste the example out of the
> online documentation and that didn't work. And then I tried running one
> of the scripts which came with IDL and that didn't work either. They
> all failed at the endfor, and didn't close the loop.
> I thought it was just me doing something stupid, but when
> the programs that came with it failed also...
> My version is the IDL student on a Win95 machine. Could the
> student editionness be the problem?
> Again, apologies for the dumb question, but this is driving me crazy,
> and it's kinda hard to program without loops!

I'm guessing that you are trying to run these FOR loops
at the IDL command line and that is what is causing
you the problems. A FOR loop is a single command to the
IDL interpreter, so you must enter it as a single command
at the IDL command line. This involves a bunch of line
continuation and line concatenation characters be used.
The loop will look something like this:

IDL> FOR j=0,4 DO BEGIN $
IDL> Print, j & $
IDL> Print, j*2 & $
IDL> ENDFOR

This is such a pain in the neck to write that no one
bothers. (Plus, if you make a typing mistake on *any*
line, you have to go back to the beginning; you can't
just fix the offending line.) Most people put this kind
of loop into a program unit of some type. For example,
you could put this loop into a main-level program by
writing it like this in a file:

FOR j=0,4 DO BEGIN
Print, j
Print, j*2
ENDFOR
END

Notice the final END at the end of the file. Name this
file "doloop.pro". To run the loop, type this:

IDL> .RUN doloop

This command actually compiles the FOR loop as a
single statement before it is run, so you don't
have to worry about line continuation and line
concatenation characters. The compiler is MUCH
smarter than the interpreter. :-)

If you want to run a compiled main-level program again,
all you have to do is this:

IDL> .Go

Another alternative is to put the loop into an IDL
procedure. All you need to do is add a procedure
definition line to the DOLOOP program, like this:

PRO DOLOOP
FOR j=0,4 DO BEGIN
Print, j
Print, j*2
ENDFOR
END

To compile and run this program you do this:

IDL> .Compile doloop
IDL> doloop

Thanks for asking this question. A lot of people
give up before admitting they don't know something. :-)

Good luck with your programming!

Cheers,

David

----------------------------------------------------------
David Fanning, Ph.D.
Fanning Software Consulting
E-Mail: davidf@dfanning.com
Phone: 970-221-0438, Toll-Free Book Orders: 1-888-461-0155
Coyote's Guide to IDL Programming: http://www.dfanning.com/

Note: A copy of this article was e-mailed to the original poster.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: clipboard objects and postscript (unix)
Next Topic: IDL and LAPACK

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 19:43:22 PDT 2025

Total time taken to generate the page: 0.00607 seconds