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

Home » Public Forums » archive » Re: how to do a loop in idl using multi-commmand?
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: how to do a loop in idl using multi-commmand? [message #43516] Sun, 17 April 2005 22:37
Dick Jackson is currently offline  Dick Jackson
Messages: 347
Registered: August 1998
Senior Member
Hi,

"lixiaoyao" <lixiaoyao5880@yahoo.com> wrote in message
news:1113584939.738412.136930@o13g2000cwo.googlegroups.com.. .
> I can run this in my idl,who know how to solve it?
> nH2=findgen(10001)*10.0+0.1
> ratio=make_array(10001)
> for i=0,10000 do
> x=2*nH2[i]
> y=x*nH2[i]
> ratio[i]=x/y
> openw,1,'file'
> printf,1,ratio
> close,1
> it seems to show you can not do multicommand in do loop.
> thanks

The suggestions that others have given are all great, but you may want to
know that you can do the work of this loop using IDL's powerful (and much
faster) array operations as follows:

nH2=findgen(10001)*10.0+0.1

x=2*nH2
y=x*nH2
ratio=x/y

or, if you don't need to make x and y for any other reason:

ratio = (2*nH2) / (2*nH2*nH2)

or then, by factoring:

ratio = 1 / nH2

A useful section in IDL Online Help that covers some features that are
different from many other languages is "Writing Efficient IDL Programs" (in
Programming in IDL:Basics of IDL Programming).

Cheers,
--
-Dick

Dick Jackson / dick@d-jackson.com
D-Jackson Software Consulting / http://www.d-jackson.com
Calgary, Alberta, Canada / +1-403-242-7398 / Fax: 241-7392
Re: how to do a loop in idl using multi-commmand? [message #43518 is a reply to message #43516] Sun, 17 April 2005 03:58 Go to previous message
Ben Panter is currently offline  Ben Panter
Messages: 102
Registered: July 2003
Senior Member
lixiaoyao wrote:
> the search website that you give me that can not be opened.can you tell
> me again?
> Thanks
>
The line was a bit broken in my newsreader, either go to

google -> groups -> comp.lang.idl-pvwave

or

http://groups-beta.google.com/group/comp.lang.idl-pvwave

which can be reached from

http://tinyurl.com/84n8l

Ben
Re: how to do a loop in idl using multi-commmand? [message #43520 is a reply to message #43518] Sat, 16 April 2005 19:40 Go to previous message
lixiaoyao is currently offline  lixiaoyao
Messages: 49
Registered: April 2005
Member
the search website that you give me that can not be opened.can you tell
me again?
Thanks
Re: how to do a loop in idl using multi-commmand? [message #43521 is a reply to message #43520] Sat, 16 April 2005 15:56 Go to previous message
R.Bauer is currently offline  R.Bauer
Messages: 1424
Registered: November 1998
Senior Member
lixiaoyao wrote:

> thank you so much for tell me this,it is really helpful too

just a hint

Sometimes it helps me to look in source code. Examples for idl source is
each routine you have already e.g. /usr/local/rsi/idl/examples/

Sometimes it would help to read the pdf manual building.pdf from /usr/local
rsi/idl/help.

Sometimes the Online Help could help if it would work, this seems to be a
never ending story ...

Sometimes a search on http://groups.google.de
groups?hl=de&lr=&ie=UTF-8&group=comp.lang.idl-pv wave helps.

Always a request to this marvellous news group helps!


cheers
Reimar


--
Forschungszentrum Juelich
email: R.Bauer@fz-juelich.de
http://www.fz-juelich.de/icg/icg-i/
============================================================ ======
a IDL library at ForschungsZentrum Juelich
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_lib_intro. html
Re: how to do a loop in idl using multi-commmand? [message #43523 is a reply to message #43521] Sat, 16 April 2005 11:41 Go to previous message
mperrin+news is currently offline  mperrin+news
Messages: 81
Registered: May 2001
Member
lixiaoyao <lixiaoyao5880@yahoo.com> wrote:
> it is still wrong. Any other suggestions?
> % Attempt to subscript NH2 with I is out of range.
> % Execution halted at: $MAIN$
> % Attempt to subscript NH2 with I is out of range.
> % Execution halted at: $MAIN$
> % Variable is undefined: Y.
> % Execution halted at: $MAIN$
>
> end

endfor, sorry.

That's what I get for thinking "This is so simple, I don't need
to test this code before I post it. " ;-)
Re: how to do a loop in idl using multi-commmand? [message #43524 is a reply to message #43523] Sat, 16 April 2005 09:29 Go to previous message
lixiaoyao is currently offline  lixiaoyao
Messages: 49
Registered: April 2005
Member
thank you so much for tell me this,it is really helpful too
Re: how to do a loop in idl using multi-commmand? [message #43525 is a reply to message #43524] Sat, 16 April 2005 09:28 Go to previous message
lixiaoyao is currently offline  lixiaoyao
Messages: 49
Registered: April 2005
Member
thanks,it works
Re: how to do a loop in idl using multi-commmand? [message #43529 is a reply to message #43525] Fri, 15 April 2005 12:55 Go to previous message
Paul Selby is currently offline  Paul Selby
Messages: 5
Registered: August 2003
Junior Member
Benjamin Hornberger wrote:
>
> If you want a FOR loop on the command line or in a batch file, you'll
> have to write it in one line, combining statements by "&":
>
> FOR I=0, 10000 DO BEGIN & x=2*nH2[i] & y=x*nH2[i] & ratio[i]=x/y & ENDFOR
>
> Note that the news client might have introduced a line break. This must
> be all in one line! I guess you realize it's better (more elegant, more
> understandable) to put it into a main-level or named program.
>
> Good luck,
> Benjamin

Actually you can put the statements on separate lines by using the line
continuation character "$" but it looks a bit messy

FOR I=0, 10000 DO BEGIN & $
x=2*nH2[i] & $
y=x*nH2[i] & $
ratio[i]=x/y & $
ENDFOR

Paul
Re: how to do a loop in idl using multi-commmand? [message #43530 is a reply to message #43529] Fri, 15 April 2005 12:49 Go to previous message
Benjamin Hornberger is currently offline  Benjamin Hornberger
Messages: 258
Registered: March 2004
Senior Member
If you do @filename, it runs as a batch file, meaning every statement,
line by line, is executed as if they were typed on the command line one
by one. You get the same error as on the command line.

What was meant in the previous post is a main-level program which has to
have an END statement and is run by ".run filename".

Go to the IDL help index and search for "batch file", then choose the
subentry "defined". You'll find a description of batch files, main-level
programs and named programs. David Fanning's book also describes it in
detail.

If you want a FOR loop on the command line or in a batch file, you'll
have to write it in one line, combining statements by "&":

FOR I=0, 10000 DO BEGIN & x=2*nH2[i] & y=x*nH2[i] & ratio[i]=x/y & ENDFOR

Note that the news client might have introduced a line break. This must
be all in one line! I guess you realize it's better (more elegant, more
understandable) to put it into a main-level or named program.

Good luck,
Benjamin

lixiaoyao wrote:
> I use @filename.pro to run it,it is still wrong. any other suggestion?
> for i=0,10000 do
> ^
> % Syntax error.
> At: /n/a/liletian/test.pro, Line 3
> % Variable is undefined: I.
> % Execution halted at: $MAIN$
> % Variable is undefined: I.
> % Execution halted at: $MAIN$
> % Variable is undefined: Y.
> % Execution halted at: $MAIN$
>
> endfor
> ^
> % Syntax error.
> At: /n/a/liletian/test.pro, Line 9
>
> end
> ^
> % Syntax error.
> At: /n/a/liletian/test.pro, Line 13
>
Re: how to do a loop in idl using multi-commmand? [message #43531 is a reply to message #43530] Fri, 15 April 2005 12:04 Go to previous message
lixiaoyao is currently offline  lixiaoyao
Messages: 49
Registered: April 2005
Member
it is still wrong. Any other suggestions?
% Attempt to subscript NH2 with I is out of range.
% Execution halted at: $MAIN$
% Attempt to subscript NH2 with I is out of range.
% Execution halted at: $MAIN$
% Variable is undefined: Y.
% Execution halted at: $MAIN$

end
^
% Syntax error.
At: /n/a/liletian/test.pro, Line 7
Re: how to do a loop in idl using multi-commmand? [message #43535 is a reply to message #43531] Fri, 15 April 2005 11:12 Go to previous message
lixiaoyao is currently offline  lixiaoyao
Messages: 49
Registered: April 2005
Member
I use @filename.pro to run it,it is still wrong. any other suggestion?
for i=0,10000 do
^
% Syntax error.
At: /n/a/liletian/test.pro, Line 3
% Variable is undefined: I.
% Execution halted at: $MAIN$
% Variable is undefined: I.
% Execution halted at: $MAIN$
% Variable is undefined: Y.
% Execution halted at: $MAIN$

endfor
^
% Syntax error.
At: /n/a/liletian/test.pro, Line 9

end
^
% Syntax error.
At: /n/a/liletian/test.pro, Line 13
Re: how to do a loop in idl using multi-commmand? [message #43536 is a reply to message #43535] Fri, 15 April 2005 10:19 Go to previous message
savoie is currently offline  savoie
Messages: 68
Registered: September 1996
Member
I think you want some block statements here.
Save this to afile.pro and try ".run afile.pro"

***************
nH2=findgen(10001)*10.0+0.1
ratio=make_array(10001)
for i=0,10000 do begin
x=2*nH2[i]
y=x*nH2[i]
ratio[i]=x/y
endfor
openw,1,'file'
printf,1,ratio
close,1
end
***************

"lixiaoyao" <lixiaoyao5880@yahoo.com> writes:

> I can run this in my idl,who know how to solve it?
> <snip/>
> it seems to show you can not do multicommand in do loop.


hth
Matt
--
Matthew Savoie - Scientific Programmer
National Snow and Ice Data Center
(303) 735-0785 http://nsidc.org
Re: how to do a loop in idl using multi-commmand? [message #43537 is a reply to message #43536] Fri, 15 April 2005 10:15 Go to previous message
mperrin+news is currently offline  mperrin+news
Messages: 81
Registered: May 2001
Member
lixiaoyao <lixiaoyao5880@yahoo.com> wrote:
> I can run this in my idl,who know how to solve it?
> nH2=findgen(10001)*10.0+0.1
> ratio=make_array(10001)
> for i=0,10000 do
> x=2*nH2[i]
> y=x*nH2[i]
> ratio[i]=x/y
> openw,1,'file'
> printf,1,ratio
> close,1
> it seems to show you can not do multicommand in do loop.

You need to add "begin" and "end" around the commands you want to loop.

nH2=findgen(10001)*10.0+0.1
ratio=make_array(10001)
for i=0,10000 do begin
x=2*nH2[i]
y=x*nH2[i]
ratio[i]=x/y
end
openw,1,'file'
printf,1,ratio
close,1
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Nice ways to compile
Next Topic: .compile vs .run

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

Current Time: Fri Oct 10 05:41:58 PDT 2025

Total time taken to generate the page: 0.55967 seconds